在canEditRowAtIndexPath方法中,UITableView的reloadData无法正常工作?

5 iphone objective-c uitableview ios

在我的应用程序,我重装我的TableView ([tablView reloadData];)从TableView中删除一行之后,那么canEditRowAtIndexPath方法送花儿给人致电(透水)总的行数.

例如:

如果我的TableView上有5行,那么我从tableView中删除1行.删除后,我重新加载我的TableView ([tablView reloadData])但是canEditRowAtIndexPath方法调用5次 而不是4次

所以我总是得到以下错误:

因未捕获的异常'NSRangeException'而终止应用程序,原因: ' *** - [__ NSArrayM objectAtIndex:]:索引5超出边界[0 .. 4]'

我也尝试在延迟(使用NSTimer)后重新加载表,但它也不适用于我.

我在这里放了一些代码:

我申请canEditRowAtIndexPath特定的行,@"status" isEqualToString:@"directory"如,

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d", self.listOfSounds.count);
    // Return NO if you do not want the specified item to be editable.
    if([[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"status"] isEqualToString:@"directory"])
        return YES;
    else
        return NO;
}  
Run Code Online (Sandbox Code Playgroud)

删除行代码:

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

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"main_id"]]; /// delete record from DB
        [self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array
        [self updateListofSoundsFile]; /// Custom method
    }
}

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO; // i also tried to  return YES;
}
Run Code Online (Sandbox Code Playgroud)

updateListofSoundsFile是我的自定义方法代码是:

-(void)updateListofSoundsFile
{
    if(self.listOfSounds.count > 0)
        [self.listOfSounds removeAllObjects];

    self.listOfSounds = [self.sql_ getAllDataFromAudioTable]; // get all record from DB
    NSLog(@"%d",self.listOfSounds.count);

    [self.tblView reloadData];  
}
Run Code Online (Sandbox Code Playgroud)

请提出任何建议,我该如何解决这个问题?
谢谢 :)

Nit*_*hel 5

你需要从tableview中删除raw也是为了从数组中删除项目并使用此行重新加载数据因为从数组中删除项目而不是tableview.

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

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

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.sql_ deleteSoundFileAudioTableWhereMainID:[[self.listOfSounds objectAtIndex:indexPath.row] objectForKey:@"main_id"]]; /// delete record from DB

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.listOfSounds removeObjectAtIndex:indexPath.row]; /// delete record from Array

        [self updateListofSoundsFile]; /// Custom method
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不应该是解决方案.1 - 在更新表之前,应始终从数据源中删除数据.2 - 如果要调用`reloadData`,则根本没有理由首先调用`deleteRowsAtIndexPath:`.此解决方案可能有效,但它隐藏了一些其他问题. (4认同)
  • 那么请指导我们什么是正确的解决方案先生请用适当的解决方案回答.谢谢 (2认同)
  • @ rmaddy-先生,如果您有其他最佳解决方案,请指导我们,以提高知识水平.谢谢 (2认同)