2009-12-18 4 views
0

Я пытаюсь удалить последний uitablviewcell из uitableview, но у меня проблемы. Код, который у меня есть до сих пор.Как удалить последний UITableViewCell

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

Который, я думал, удалит последнюю ячейку? Какие-либо предложения? Благодарю.

+0

второй параметр '-deleteRowsAtIndexPaths: withRowAnimation:' не является 'BOOL'; это тип 'UITableViewRowAnimation' – user102008

ответ

1

Число строк в разделе не основано на 0 (то есть оно вернет 1, хотя indexPath.row == 0). Попробуйте 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]; 
Смежные вопросы