Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dequeueReusableCellWithIdentifier returning cells in editing state in IOS 7

Tags:

uitableview

The following problem doesn't occur in IOS versions prior to 7.

Using UITableView's slide-to-edit interface for deleting items, after deleting an item and scrolling, the newly displayed cell (returned from dequeueReusableCellWithIdentifier) looks like this:

https://dl.dropboxusercontent.com/u/87749409/Screenshot%202013.09.27%2016.08.30.png

[That image may not be available forever, so here's a description: After scrolling, the new cell is still in the final edited state, with the DELETE button still visible and the cell contents off-screen to the left.]

Furthermore, the returned cell is even has its editing flag set.

The code I'm using to delete cells looks like this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Find the item that this cell represents
        NSDictionary *item = [self itemForRowAtIndexPath:indexPath];
        if (!item) return;

        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [tableView endUpdates];

        // Remove it from the data store
        [Inventory removeInventoryItem:item];
    }
}

I was able to overcome this with a hacked solution. The code (with hack) is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InventoryCell *cell = [tableView dequeueReusableCellWithIdentifier:kInventoryCellID];

    // HACK - here we create a new cell when we try to reuse deleted cells. 
    // Deleted cells, when re-used, would still appear as if they were edited,
    // with the cell slid off to the far left and the DELETE button visible.
    while(cell && cell.editing)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:kInventoryCellID];
    }

    if (!cell)
    {
        cell = [[InventoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kInventoryCellID];
    }

    return cell;
}

With the hack (the while loop) a new cell is generated which is not in the editing state. This leads to a bit of wasted memory, but does produce the correct result.

I wonder if there is there something that I need to do in the prepareForReuse method to reset the editing state. Currently, my prepareForReuse method only initializes the controls inside (labels, etc.) with default values.

I've tried calling setEditing:animated: on both, the UITableViewCell and the UITableView when deleting the cell, in the prepareForReuse and whenever dequeueReusableCellWithIdentifier: returned a cell that still had the editing flag set, but nothing seems to solve the problem.

like image 759
pauln Avatar asked Sep 27 '13 21:09

pauln


People also ask

Why we use dequeueReusableCellWithIdentifier in ios?

When we use, dequeueReusableCellWithIdentifier, the tableView just creates exactly the number of cells based on your table height and cell height. Suppose, if it shows 4 cells in the tabelView and rest you can see by scrolling, then memory for only 4 cells would be allocated at any given point of time.

What is prototype cell?

A prototype cell acts a template for your cell's appearance. It includes the views you want to display and their arrangement within the content area of the cell. At runtime, the table's data source object creates actual cells from the prototypes and configures them with your app's data.

What is DequeueReusableCell?

DequeueReusableCell(String) Returns a reusable table view cell that was created with the given ReuseIdentifier. DequeueReusableCell(NSString, NSIndexPath) Returns a reusable table view cell for the given reuseIdentifier , properly sized for the indexPath .


1 Answers

I had this issue, and the answer for me was a "doh" moment. Make sure you call the super implementation of prepareForReuse in your own implementation.

like image 91
Léo Natan Avatar answered Jan 03 '23 11:01

Léo Natan