【发布时间】:2012-11-26 12:30:45
【问题描述】:
使用编辑/删除或滑动删除手势删除项目后,我的表格没有更新。如果我进行编辑/删除,删除按钮不会消失。它以按下/卡住的状态停留在那里。然后当我单击完成时,删除按钮消失。这篇文章的底部附有一个截图。
我的 numberOfRowsInSection 代码在应用启动时执行,而不是在删除项目后执行。
我正在使用 UITableViewController。我的 .h 文件中的定义:
@interface BNVFavoritesTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
BNVAppDelegate *appDelegate;
IBOutlet UITableView *myTable;
}
@property (retain, nonatomic) IBOutlet UITableView *myTable;
@end
这是我的 .m 文件中的相关代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"number of rows:\t%d", [appDelegate.favoritesArray count]);
return [appDelegate.favoritesArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Get the object from the array.
BNVFavorite *favoriteObj = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
//Set the name.
cell.textLabel.text = favoriteObj.name;
// Set up the cell
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete){
NSLog(@"before delete\t%d", [appDelegate.favoritesArray count]);
BNVFavorite *selectedObject = [appDelegate.favoritesArray objectAtIndex:indexPath.row];
NSString *selectedName = selectedObject.name;
// delete from sqlite database
[appDelegate removeFavoriteFromDB:selectedName];
// delete from memory array
[appDelegate.favoritesArray removeObjectAtIndex:indexPath.row];
// delete in user interface
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
NSLog(@"after delete\t%d", [appDelegate.favoritesArray count]);
}
}
删除时,我在日志中看到“删除前”和“删除后”消息(如预期的那样),但没有“行数”消息。
表视图中的数据源和委托出口连接到 TableViewController 类。我也尝试在 ViewDidLoad 方法中执行此操作,但这并没有什么不同。在我的commitEditingStyle 末尾强制使用[myTable reloadData]; 也无济于事。
最后,这是那个“卡住”删除按钮的屏幕截图。 http://i.stack.imgur.com/ukUu0.png
点击几次会导致 NSRangeException/out of bounds 错误,表示执行了来自commitEditingStyle的代码。
【问题讨论】:
-
嘿,在 appDelegate.favoritesArray 中,对象是否被删除?
-
是的,已删除。如果我点击删除按钮几次,我得到 NSRangeException 错误,正如预期的那样。
-
您的行是否已从表中删除..?
-
是的,但那不是问题。
-
我在回答中提到了你的回答..:)
标签: iphone xcode uitableview