UITableView: after deleting rows and adding new ones, no new rows are selected

I have a pretty normal UIViewController

s UITableView

, I should have done this hundreds of times before. The only difference is that my table can only contain rows added or removed when in edit mode (not sure if my problem is related to this).

The table shows the main data. I am using Xcode 6.2, targeting iOS 8.2. The problem occurs on the simulator and devices.

The problem is that every time I delete rows and then add new ones, the new rows appear in the table as expected , but they cannot be selected .

tableView:didSelectRowAtIndexPath:

and are tableView:willSelectRowAtIndexPath:

not called. The rest of the lines are just fine.

This made me drift apart for a whole day now, and I thought it was a weird inconsistency at first, but once I identified the failure pattern, it is 100% consistent.

Interestingly, after the application is closed and restarted, the inserted rows are selectable, so I'm pretty sure the problem is with the internal state of the table and not with the data source.

I've tried everything I could think of, including being free to post calls [self.tableView reloadData]

after everything and explicitly re-setting self.tableView.allowsSelectionDuringEditing = YES

.

Some of the code is inserted below. (I have nested all the methods I could think of that seem to be related to the error.)

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.allowsSelectionDuringEditing = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSLog(@"pages count %d", [self pages].count);
    return [self pages].count + (tableView.editing ? 1 : 0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < [[self pages] count])
    {
        PageListTableViewCell *cell = (PageListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kDefaultCellReuseIdentifier forIndexPath:indexPath];
        PageModel *page = [self pages][indexPath.row];
        // Configure the cell...
        cell.displayNameLabel.text = page.displayName;
        //cell.editing = [page.displayName isEqualToString:@""];
        return cell;
    }
    else if (tableView.editing)
    {
        AddTableViewCell *cell = (AddTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kAddCellReuseIdentifier forIndexPath:indexPath];
        cell.textLabel.hidden = YES;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < [[self pages] count])
    {
        // ... normal selection behaviour
    }
    else
    {
        // add new row
        [self addPageAtIndexPath:indexPath];
    }
}

- (void)addPageAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView beginUpdates];
    // add row in core data
    NSString *pageID = [NSString stringWithFormat:@"page-%ld-%@", indexPath.row, [[NSUUID UUID] UUIDString]];
    PageModel *page = [[PageModelController sharedInstance] addPageWithIdentifier:pageID displayName:@"untitled"];
    NSLog(@"Page %ld added, ID %@", indexPath.row, pageID);

    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.tableView beginUpdates];
        // Delete the row from core data
        PageModel *page = [self pages][indexPath.row];
        [[PageModelController sharedInstance] removePage:page];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
}

      

+3


source to share


1 answer


Finally found this - thanks to @Rory McKinnel for making me take a closer look at my own implementation of cells. This did almost nothing because during my debugging efforts I got rid of the gut, but unfortunately it still remained prepareForReuse

and there was a call missingsuper

. Uch.



+2


source







All Articles