在我的应用程序中,我在从([tablView reloadData];)中删除行后重新加载TableView TableView,然后canEditRowAtIndexPath方法总是调用(透透)的总行数。
例如,:
如果我的5行位于TableView上,则从tableView中删除1行。删除后,我重新加载 my TableView ,但canEditRowAtIndexPath方法调用5 time而不是4次?
因此,我总是得到以下错误:
原因: '* *-_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;
} 删除行代码:
- (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;
}这里,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];
}请给我一些建议,我该如何解决这个问题?
谢谢:)
发布于 2013-08-23 04:59:09
您需要从tableview中删除原始项,然后从数组中删除项,并使用这一行重新加载数据,因为从数组中删除项,而不是从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
}
}发布于 2013-10-30 02:44:09
我遇到了同样的问题。问题是,我删除了单元格的对象,但是当我使用tableView的tableView方法时,没有调用我的canEditRowAtIndexPath方法,从而可以编辑我不想编辑的单元格。真正的修正不是调用deleteRowsAtIndexPaths方法,而是调用[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];方法。
从根本上讲,这里发生的事情是:
当我调用reloadData时,原始数据并没有像Nitin所说的那样从tableView中删除。因此,这不是解决办法。
当我调用deleteRowsAtIndexPaths时: iOS检测到底层数据和单元格数之间的差异(因为我已经删除了基础对象)。其结果是崩溃,这也不是解决办法(显然)。
现在是修理!
当我调用reloadRowsAtIndexPaths时:这导致tableView简单地重新加载单个单元格,并且它去掉了原始数据。这就是解决办法。
与其删除dataSource对象,然后尝试删除本质上没有任何支持的单元格(这会导致崩溃),只需删除dataSource对象,然后重新加载tableView的indexPath。
以下是该方法的一般格式:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[myDataSourceArray removeObjectAtIndex:indexPath.row];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}这是我第一次遇到没有通过调用reloadData方法删除的剩余原始数据的问题。这无疑是我迄今所见过的最优雅的解决办法。
编码愉快!我希望这能帮到你。
发布于 2013-08-23 04:52:55
- (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
}
[self.tblView reloadData];
}https://stackoverflow.com/questions/18394816
复制相似问题