Skip to content

Conditional Cells Improperly Updating #47

@mattjoers

Description

@mattjoers

I am trying to add some conditional logic in my data feed, here is how the sample data file looks:

ExampleData.swift:

public struct Item {
    var name: String
    var active: Bool
    var icon: String?
    var position: String?
    
    public init(name: String, active: Bool, icon: String?, position: String?) {
        self.name = name
        self.active = active
        self.icon = icon
        self.position = position
    }
}

public struct Section {
    var name: String
    var items: [Item]
    var collapsed: Bool
    
    public init(name: String, items: [Item], collapsed: Bool = false) {
        self.name = name
        self.items = items
        self.collapsed = collapsed
    }
}

public var sectionsData: [Section] = [
    Section(name: "Profile", items: [
        Item(name: "UsersFirstName", active: false,  icon: "none",  position: "left"),
        Item(name: "EmailAddress", active: false, icon: "none", position: "left"),
        Item(name: "Change your email", active: true, icon: "chevron", position: "left"),
        Item(name: "Change your password", active: true, icon: "chevron", position: "left")
    ]),
    Section(name: "Support", items: [
        Item(name: "Call us", active: true, icon: "call", position: "left"),
        Item(name: "Provide feedback", active: true, icon: "feedback", position: "left"),
        Item(name: "Help", active: true, icon: "help", position: "left")
    ]),
    Section(name: "Learn More Info", items: [
        Item(name: "Terms and Conditions", active: true, icon: "chevron", position: "left"),
        Item(name: "Version #.##.#", active: false, icon: "none", position: "center")
    ])
]

Then in the table, I'm trying to set the cells to have conditional formatting as depicted below (whether its active OR if the the text should be centered):

CollapsibleTableViewController.swift:

   // Cell
   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CollapsibleTableViewCell ??
            CollapsibleTableViewCell(style: .default, reuseIdentifier: "cell")
        
        let item: Item = sections[indexPath.section].items[indexPath.row]
            print("items: \(item)")
        cell.nameLabel.text = item.name
        cell.accessoryView = UIImageView(image:UIImage(named: (item.icon ?? nil)!))
        
        if (item.icon == "none") {
            cell.nameLabel.textColor = UIColor(hex: 0xA9A9A9)
            cell.isUserInteractionEnabled = false
        }

        switch item.position {
        case "center":
            cell.nameLabel.textAlignment = .center
        case "left":
            cell.nameLabel.textAlignment = .left
        default:
            cell.nameLabel.textAlignment = .left
        }
    
        return cell
    }

However, when I run everything, it looks correct however, once I start expanding and collapsing the last section, the first item "Terms and Conditions" is not enabled.

Is there something I am missing, such as a "refresh" or am I not properly loading the data? Any assistance or suggestions would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions