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

时间:2012-02-12 17:17:51

标签: iphone objective-c ios 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];
 }

2 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}和reloadData内拨打beginUpdates。您始终可以在endUpdates内将布尔标志设置为true,并在致电beginUpdates后检查该标志是否应该调用endUpdates,尽管我不确定您为什么需要致电reloadData

答案 1 :(得分:0)

我想我可能知道发生了什么,所以我打算将其作为答案发布。

我刚刚注意到你要求listOfItems获取你从中移除项目的临时数组。我还看到你正在更新NSUserDefaults中的一个对象。完成此操作后,listOfItems是否也已更新?如果不是,那就是问题所在。它将继续保留与之前相同的所有信息,从而看起来好像没有更新发生。我希望情况就是这样,因为它将为您提供一个非常简单的解决方案。如果不是,请查看您的dataSource方法,以了解问题所在。