Anonymous-E
Anonymous-E

Reputation: 827

Swift Tableview get specific cell not working

I'm trying to clear a specific cell for my tableview. I know to achieve that by using this code right here:

let index = IndexPath(row: 1, section: 0)
if let cell = self.tableView.cellForRow(at: index) as? SomeTableViewCell {
     // do sth
  }else {
     print("Row 1 not found")
}

But the problem is when I use this method with a bigger screen phone like Iphone7, 8, X... It'll be working fine but when I tested on iphone5, 6 or the smaller screen that Row 1 is not visible, It'll print that "Row 1 not found"

Upvotes: 0

Views: 203

Answers (1)

Ruchira Randana
Ruchira Randana

Reputation: 4179

Seems like you've almost found the problem yourself!

As with tableviews, the iOS reuses cells them to preserve memory and to offer a seamless smooth scrolling experience to the user.

The iOS reuses the cells when they disappear from the screen. ie: When you scroll down, the initial cells start to be reused. In your larger screens, this takes a while to occur as it may be displaying 20 cells at once vs 10 screens (at once) on your smaller screens.

If you want to permanently remove this cell, then you should remove the corresponding data object from your model which backs up the table view (datasource) and reload the tableview.

If you don't want to permanently remove this cell, then you can reset it only when the table actually needs it to be drawn. This can be done in the datasource method

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
            var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
//Do your Resetting code here
}

Upvotes: 2

Related Questions