【发布时间】:2013-08-23 04:39:43
【问题描述】:
在我的应用程序中,我在从 TableView 删除行之后重新加载我的 TableView ([tablView reloadData];) 然后 canEditRowAtIndexPath 方法总是调用 (pervious) 总行数.
例如:
如果我的 TableView 上有 5 行,那么我从 tableView 中删除 1 行。删除后,我 重新加载 我的 TableView ([tablView reloadData]) 但 canEditRowAtIndexPath 方法调用 5 次 而不是 4 次强>??
所以我总是得到关注错误:
由于未捕获的异常“NSRangeException”而终止应用程序,原因: '* **-[__NSArrayM objectAtIndex:]: index 5 beyond bounds [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];
}
请提供任何建议,我该如何解决这个问题?
谢谢:)
【问题讨论】:
-
发布您的
numberOfRowsInSection方法。问题 - 在您的commitEditingStyle方法中,为什么要调用updateListOfSoundsFile?您已经通过删除一个对象更新了listOfSounds。从数组中删除对象后,只需调用deleteRowsAtIndexPaths从表中删除一行。还要验证您对deleteSoundFileAudioTableWhereMainID的调用实际上是从数据库中删除一行。
标签: iphone ios objective-c uitableview