繁体   English   中英

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

[英]UITableView cells are not selected after deleting last cell

我将UITableView分为三个部分:三个部分中的两个是常数(每个常数只有一个不变的单元格),第一部分是您可以在单元格中添加或删除的部分。 除了从该部分删除单元格外,其他所有功能都可以正常运行:一旦删除了最后一个单元格,此单元格便消失了(如预期的那样),但其他部分则无法正常工作,这意味着我无法单击它们(它们均加载另一个视图)。 如果我在第一部分为空时加载应用程序,那么一切都很好。 仅在删除此部分的最后一个单元格后,此事件才会发生。 我试图将日志放置在代码中的任何地方,并且一切正常。

将不胜感激任何建议。

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

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

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

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

我只是注意到您正在调用listOfItems以获得从中删除项目的临时数组。 我还看到您正在更新NSUserDefaults的对象。 完成此操作后, listOfItems也会更新? 如果不是,那就是您的问题所在。 它将继续保存与以前相同的所有信息,因此看起来好像没有更新发生。 我希望情况确实如此,因为这将使您的修复非常容易。 如果不是这样,可能有必要查看您的dataSource方法以查看问题所在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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