UITableView deleteRowsAtIndexPaths不删除行 [英] UITableView deleteRowsAtIndexPaths doesn't delete row
本文介绍了UITableView deleteRowsAtIndexPaths不删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有UIView设计在IB,和UITableView上。在视图contoller的viewDidLoad方法中我init我的自定义UITableViewContoller并设置dataSource和委托:
I have UIView designed in IB, and UITableView on it. In view contoller's viewDidLoad method I init my custom UITableViewContoller and sets dataSource and delegate:
- (void)viewDidLoad {
[super viewDidLoad];
...
GoodsTableController *goodsController = [[GoodsTableController alloc] init];
goodsController.data = [[orderData objectForKey:@"Result"] objectForKey:@"Items"];
self.gtc = goodsController;
[goodsController release];
goodsTable.delegate = self.gtc;
goodsTable.dataSource = self.gtc;
[goodsTable reloadData];}
Intenting in GoodsTableController:
//in the header file
@interface GoodsTableController : UITableViewController {
NSMutableArray *data;
}
@property (nonatomic, retain) NSMutableArray *data;
//implementation
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Current data size %d", [data count]);
return [data count];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"Deleting row at index %d", indexPath.row);
[self.tableView beginUpdates];
[self.data removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
[self.tableView reloadData];
}
}
对于debug我使用NSLog,改变了,它的大小改变了,tableView:numberOfRowsInSection被调用,但表不更新。 这是日志:
2011-04-07 13:08:18.784 Test[32300:207] Current data size 2 //first call tableView:numberOfRowsInSection: by reloadData in View Controller after set data
2011-04-07 13:08:23.486 Test[32300:207] Current data size 2 //call after slide on row
2011-04-07 13:08:26.091 Test[32300:207] Deleting row at index 0
2011-04-07 13:08:26.093 Test[32300:207] Current data size 1 //call after endUpdate
2011-04-07 13:08:26.096 Test[32300:207] Current data size 1 //call after reloadData
推荐答案
问题出现在GoodsTableController实例中未定义的tableView中。
固定代码:
The problem was in undefined tableView in GoodsTableController instance. Fixed code:
- (void)viewDidLoad {
[super viewDidLoad];
...
GoodsTableController *goodsController = [[GoodsTableController alloc] init];
goodsController.data = [[orderData objectForKey:@"Result"] objectForKey:@"Items"];
self.gtc = goodsController;
[goodsController release];
goodsTable.delegate = self.gtc;
goodsTable.dataSource = self.gtc;
//Fix there:
self.gtc.tableView = goodsTable;
[goodsTable reloadData];
}
这篇关于UITableView deleteRowsAtIndexPaths不删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文