kitchen800
kitchen800

Reputation: 227

Active tableView cells keep getting disabled when I scroll through my tableview

I have app that has a UITableView containing different sections. I want to have only allow access to the first 3 sections i.e the index path 0, 1 and 2. My problem is my code works when the app is launched. However when I scroll down through the table view sections and scroll back up the top of the tableview sections 0, 1 and 2 are disabled when I come back up to them. how can I fix this?

//formatting the cells that display the sections
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!

    cell.textLabel?.text = sectionName[indexPath.row]
    cell.textLabel?.textAlignment = .Center
    cell.textLabel?.font = UIFont(name: "Avenir", size:30)

    //Code to block disable every section after row 3.
    if ( indexPath.row  >= 2 ) {
    cell.userInteractionEnabled = false
    cell.contentView.alpha = 0.5
    }

    return cell

}

Upvotes: 0

Views: 151

Answers (1)

ebby94
ebby94

Reputation: 3152

The cells are being reused. The cells are reused and are not create again to improve performance. So when you scroll down, the interaction for the cells will be disabled because of your condition check. Since there isn't a condition to check if the indexPath.row below 2, the user interaction stays the same(false) from the reused cell.

Just a small modification to your condition check will fix it.

if ( indexPath.row  >= 2 ) {
    cell.userInteractionEnabled = false
    cell.contentView.alpha = 0.5
}
else{
    cell.userInteractionEnabled = true
    cell.contentView.alpha = 1
}

Upvotes: 2

Related Questions