简体   繁体   中英

UITableView cells are not selected after deleting last cell

I have a grouped UITableView with 3 sections: 2 of the 3 are constants (each with only one cell that doesn't change) and the first section which you can add or remove from cells. everything works perfectly besides when deleting cells from this section: once i deleted the last cell, this cell disappears (as expected) but then the other sections are not functional, meaning, i cant click on them (they both load another view). if i load the app when the first section is empty in the first place, everything is fine. this thing happens only after deleting the last cell in this section. i tried to put logs everywhere in the code and everything looks ok.

Would appreciate any suggestion.

Here is some code that may be relevant. happy to provide more if needed:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0) {
    return YES;
 } else {
    return NO;
 }
}


- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

[tblSimpleTable beginUpdates];

if (editingStyle == UITableViewCellEditingStyleDelete) {

     NSMutableArray * section = [listOfItems objectAtIndex:indexPath.section];    
    [section removeObjectAtIndex:indexPath.row];

    //UPDATING USERS DEFAULTS
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];            
    [defaults setObject:section forKey:@"listOfAccessNumbers"];

    [tblSimpleTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    [tblSimpleTable reloadData];


    }


}     
else if (editingStyle == UITableViewCellEditingStyleInsert) {

...

}

[tblSimpleTable endUpdates];

}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated {

NSLog(@"****************** entering setEditing");

[super setEditing:editing animated:animated];
[tblSimpleTable setEditing:editing animated:YES];
if (editing) {
     editButton.enabled = NO;
} else {
    editButton.enabled = YES;
}
}

UPDATE: adding more methods:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"****************** entering cellForRowAtIndexPath");

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; //try here diff styles
}


NSArray *array = [[NSArray alloc] init];
array = [listOfItems objectAtIndex:indexPath.section];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.textColor = [UIColor colorWithRed:.196 green:0.3098 blue:0.52 alpha:1.0];

if (sectionIndicator == YES){

    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else if (indexPath.section == 1) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

}
else
{
    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if (indexPath.section == 1) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}


return cell;
}




 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [listOfItems count];    
 }



 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [[listOfItems objectAtIndex:section] count];
 }

Don't call reloadData inside beginUpdates and endUpdates . You can always set a boolean flag to be true inside beginUpdates and check that flag after you call endUpdates to see if you should call reloadData , although I'm not sure why you need to call reloadData .

I think I may know what's going on, so I'm going to post it as an answer.

I just noticed that you are calling for listOfItems to get the temp array which you remove the item from. I also see you are then updating an object in the NSUserDefaults . After this is done, is listOfItems also updated? If it isn't, that is where you problem lies. It will continue to hold all the same information as before, thus appearing as if there's no update occurring. I hope this is the case, because it will make for a very easy fix on your part. If it isn't, seeing your dataSource methods might be necessary to see where the problem might lie.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM