iOS 6th Edition CheckList

I’ve tried this Checklist section four times and made sure I followed each step, so last time I did, I used your step code, but still having issues, can someone look this over and help me ?

import UIKit

class ChecklistViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func tableView(_ tableView: UITableView,
                        numberOfRowsInSection section: Int) -> Int {
    return 100
}

override func tableView(_ tableView: UITableView,
                        cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(
        withIdentifier: "ChecklistItem", for: indexPath)
    
    let label = cell.viewWithTag(1000) as! UILabel        Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
    
    if indexPath.row % 5 == 0 {
        label.text = "Walk the dog"
    } else if indexPath.row % 5 == 1 {
        label.text = "Brush my teeth"
    } else if indexPath.row % 5 == 2 {
        label.text = "Learn iOS development"
    } else if indexPath.row % 5 == 3 {
        label.text = "Soccer practice"
    } else if indexPath.row % 5 == 4 {
        label.text = "Eat ice cream"
    }
    
    return cell
}

override func tableView(_ tableView: UITableView,
                        didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        if cell.accessoryType == .none {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
    }
    
    tableView.deselectRow(at: indexPath, animated: true)
}

}

Ok, I found I forgot to put any amount at the View tag in Label.

TY

@billj Glad you sorted it out! Cheers! :]

This topic was automatically closed after 166 days. New replies are no longer allowed.