Search code examples
iosswiftuitableviewguard

Customize the UITableViewCell text inside a guard statement


I have a tableview that reads information from an array of arrays and displays it. The tableview has collapsable cells for subcategories. I can't seem to figure out how to make my TableView text conform to the prototype cell I created. I created a .swift file for the cell named "autoClaimCell", the cell identifier is "autoClaimCell" as well, and the label inside that cell is called "autoClaimCellLabel". Before I made a guard statement, it worked fine because it would allow me to place "as! autoClaimCell" at the end of the statement, which then accessed that file for the label, but now since I have the guard statement it won't let me place "as! autoClaimCell" anywhere in there.

Here's my code:

    func numberOfSections(in tableView: UITableView) -> Int {
        return tableViewData.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableViewData[section].opened == true {
            return tableViewData[section].sectionData.count + 1
        } else {
            return 1
        }
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "autoClaimCell", for: indexPath) as! autoClaimCell
//        cell.autoClaimCellLabel?.text = areaCategories[indexPath.row]
//        return cell
        let dataIndex = indexPath.row - 1

        if indexPath.row == 0 {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "autoClaimCell") else {return UITableViewCell()}
            cell.textLabel?.text = tableViewData[indexPath.section].title
            return cell
        } else {
            //Use different call indentifier if needed
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "autoClaimCell") else {return UITableViewCell()}
            cell.textLabel?.text = tableViewData[indexPath.section].sectionData[dataIndex]
            return cell
        }

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if indexPath.row == 0 {

            if tableViewData[indexPath.section].opened == true {
                tableViewData[indexPath.section].opened = false
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none) // play around with animations
            } else {
                tableViewData[indexPath.section].opened = true
                let sections = IndexSet.init(integer: indexPath.section)
                tableView.reloadSections(sections, with: .none) // play around with animations
            }
        }
    }

Also, how would I make the listing that are shown when the cell is expanded conform to a different cell prototype in the process?


Solution

  • The expression in the guard-let statement should evaluate to an optional. Using as! will force unwrap the optional which will either succeed or generate a fault but never return an optional. Just replace as! with as?

    As for conforming to a different cell protototype. You need to manage that with your data model. You already have different conditions for index row 0. You can add conditions for if expanded or such like.