Search code examples
swiftuitableviewcellgenerated

Stop table from generating cell that has already been deleted


This is the code that I use to generate a regular table cell in my RSS feed reader:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Function adds the title to each cell
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    let item = feedItems[indexPath.row] as MWFeedItem

    cell.textLabel?.text = item.title
    return cell

}

I want to add an if statement to the above function which will stop the function from generating the table cell in the case that the feed has already been deleted by the user. I know how to check if it has been deleted (what would go inside the parenthesis after the if), but not what I would put inside the actual if statement.

if(/*table cell has been deleted*/)
{
/*code that stops this feed from being regenerated*/
}

Solution

  • The way to do this is to actually delete or remove the item from feedItems and then reload the tableView using tableView.reloadData(). This will then provide the correct number of elements to the tableView only draw all the elements contained in feedItems.

    Hope this helps.