删除最后一个单元格后,未选择UITableView单元格



我有一个分组的UITableView,它有3个部分:3个部分中的2个是常量(每个部分只有一个不变的单元格),第一个部分可以添加或从单元格中删除。除了从这个部分删除单元格外,一切都很好:一旦我删除了最后一个单元格,这个单元格就会消失(正如预期的那样),但其他部分也不起作用,这意味着我无法点击它们(它们都加载了另一个视图)。如果我在第一部分为空时加载应用程序,那么一切都很好。只有在删除该节中的最后一个单元格后,才会发生这种情况。我试着在代码中到处放日志,一切看起来都很好。

如有任何建议,不胜感激。

以下是一些可能相关的代码。如果需要,很乐意提供更多:

- (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;
}
}

更新:添加更多方法:

 - (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];
 }

我想我可能知道发生了什么,所以我要把它作为答案发布。

我刚刚注意到,您正在调用listOfItems来获取从中删除该项的临时数组。我还看到您正在更新NSUserDefaults中的一个对象。完成此操作后,是否也更新了listOfItems?如果不是,那就是问题所在。它将继续保持与以前相同的信息,因此看起来好像没有发生更新。我希望是这样,因为这将使你很容易解决问题。如果不是,那么查看dataSource方法可能有必要了解问题所在。

不要在beginUpdatesendUpdates内部调用reloadData。您总是可以在beginUpdates中设置一个布尔标志为true,并在调用endUpdates后检查该标志,看看是否应该调用reloadData,尽管我不确定为什么需要调用reloadData

最新更新