2009-12-18 89 views
0

我想从uitableview中删除最后一个uitablviewcell,但我遇到了一些麻烦。我到目前为止的代码是。如何删除最后一个UITableViewCell

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[self.tableView numberOfRowsInSection:0]] withRowAnimation:NO]; 

我以为会删除最后一个单元格?有什么建议么?谢谢。

+0

的'-deleteRowsAtIndexPaths第二个参数:withRowAnimation:'不是'BOOL';它是一个'UITableViewRowAnimation'类型 – user102008 2011-04-27 23:36:36

回答

1

该部分中的行数不是0(即使indexPath.row == 0它将返回1)。尝试arrayWithObject:([self.tableView numberOfRowsInSection:0] - 1)。

此外,[self.tableView numberOfRowsInSection:]返回一个NSInteger,当数组确实需要一个NSIndexPath对象时。

1

假设你只有1节在表中,这应该这样做,这取决于你希望把这个代码:

NSInteger temprowcount = [self.tableView numberOfRowsInSection:0]; 
if (temprowcount > 0) { 
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:temprowcount-1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; 
} 
0

如果你有一个以上的部分,假设self是视图控制器包含tableView和符合UITableViewDataSource协议:

NSUInteger _lastSection = [self numberOfSectionsInTableView:tableView]; 
NSUInteger _lastRow = [tableView numberOfRowsInSection:_lastSection] - 1; 
NSUInteger _path[2] = {_lastSection, _lastRow}; 
NSIndexPath *_indexPath = [[NSIndexPath alloc] initWithIndexes:_path length:2]; 
NSArray *_indexPaths = [[NSArray alloc] initWithObjects:_indexPath, nil]; 
[_indexPath release]; 

[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:_indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
[tableView endUpdates]; 

[_indexPaths release];