我在IB中设计了UIView,并在其上设置了UITableView。在视图contoller的viewDidLoad方法中,我初始化我的自定义UITableViewContoller并设置dataSource和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];}
在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];
}
}
对于调试,我使用了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
答案 0 :(得分:1)
我研究了UITableView的删除,插入和放大的机制。重装。我在Github维护我的项目来解决这个问题。
https://github.com/VansonLeung/VanTableController
它包括一个工作单元测试。通常,为了动画UITableView的单元格动画,我们必须执行以下操作:
[tableView_ beginUpdates];
[tableView_ deleteRowsAtIndexPaths: indexPathDelete withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView_ insertRowsAtIndexPaths: indexPathInsert withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView_ reloadRowsAtIndexPaths: indexPathReload withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView_ endUpdates];
我们必须确定删除行,插入行和&重新加载行的indexPath数组,这是一个非常讨厌的工作!我的项目工作基本上是生成这些indexPaths以使生活更轻松的包装器。
答案 1 :(得分:0)
试试这个
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [NSMutableArray count];
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.text = [NSMutableArray objectAtIndex:indexPath.row];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!indexPath) return
{
UITableViewCellEditingStyleNone;
}
if (indexPath.row == ([NSMutableArray count]))
{
return UITableViewCellEditingStyleInsert;
}
else
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Code for do anything after select row in table view
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[recordedFileList removeObjectAtIndex:indexPath.row];
[recordTableView reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [[NSMutableArray objectAtIndex:fromIndexPath.row] retain];
[NSMutableArray removeObject:item];
[NSMutableArray insertObject:item atIndex:toIndexPath.row];
[item release];
}
答案 2 :(得分:0)
问题出在GoodsTableController实例中未定义的tableView中。 固定代码:
- (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];
}