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

2023-11-26

在我的应用程序中,我重新加载 TableView([tablView reloadData];)从 TableView 中删除行后canEditRowAtIndexPath方法总是调用(透水)总行数。

例如:

如果我有5我的 TableView 上的行,然后我删除1来自 tableView 的行。删除后我reload我的表格视图([tablView reloadData]) but canEditRowAtIndexPath方法调用5 time代替4 times ??

所以我总是得到关注Error:

由于未捕获的异常“NSRangeException”而终止应用程序,reason: '* **-[__NSArrayM objectAtIndex:]: 索引 5 超出范围 [0 .. 4]'

我还尝试在延迟一段时间后重新加载表(使用NSTimer)但它对我也不起作用。

我在这里放了一些代码:

I apply 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;
}  

删除行代码:

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

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

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


在从数组中删除项目并使用此行重新加载数据之前,您还需要从表视图中删除原始数据,因为从数组中删除项目而不是表视图。

[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
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在canEditRowAtIndexPath方法中,UITableView的reloadData无法正常工作? 的相关文章

随机推荐