 import UIKit
 
 class CheckListViewController: UITableViewController {
-    var row0item: ChecklistItem
-    var row1item: ChecklistItem
-    var row2item: ChecklistItem
-    var row3item: ChecklistItem
-    var row4item: ChecklistItem
+    var items = [ChecklistItem]()
     
     // MARK: - init
     required init?(coder aDecoder: NSCoder) {
-        row0item = ChecklistItem()
+        let row0item = ChecklistItem()
         row0item.text = "Walk the dog"
         row0item.checked = false
+        items.append(row0item)
         
-        row1item = ChecklistItem()
+        let row1item = ChecklistItem()
         row1item.text = "Brush my teeth"
         row1item.checked = true
+        items.append(row1item)
         
-        row2item = ChecklistItem()
+        let row2item = ChecklistItem()
         row2item.text = "Learn iOS development"
         row2item.checked = true
+        items.append(row2item)
         
-        row3item = ChecklistItem()
+        let row3item = ChecklistItem()
         row3item.text = "Soccer practice"
         row3item.checked = false
+        items.append(row3item)
         
-        row4item = ChecklistItem()
+        let row4item = ChecklistItem()
         row4item.text = "Eat ice cream"
         row4item.checked = true
+        items.append(row4item)
         
         super.init(coder: aDecoder)
     }

    
    // MARK: - helper
    func configureCheckmarkForCell(cell: UITableViewCell, indexPath: NSIndexPath) {
        var isChecked = false
        
        if indexPath.row == 0 {
            isChecked = row0item.checked
        } else if indexPath.row == 1 {
            isChecked = row1item.checked
        } else if indexPath.row == 2 {
            isChecked = row2item.checked
        } else if indexPath.row == 3 {
            isChecked = row3item.checked
        } else if indexPath.row == 4 {
            isChecked = row4item.checked
        }
        
        if isChecked {
            cell.accessoryType = .Checkmark
        } else {
            cell.accessoryType = .None

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem", forIndexPath: indexPath)
        
        let item = items[indexPath.row]
        let label = cell.viewWithTag(1000) as! UILabel
        label.text = item.text
        
        configureCheckmarkForCell(cell, indexPath: indexPath)
        
