2009-12-18 93 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];