UITableView - Select nearest row after row deletion
like build in apple notes app, after you swipe to delete the selected row, it will select the nearest available row automatically.
The logic should be:
if row count > 0 then if deleted_row == last row then
select deleted_row_index-1 row else select deleted_row_index+1 row end end
i have try to 开发者_运维百科implement the above logic in the commitEditingStyle event, but the selection fail.
the selectRowAtIndexPath logic just don't work in this event, if i apply it in a button, it works.any idea?
One way to solve this is to use the method performSelector:withObject:afterDelay:
.
From within the tableView:commitEditingStyle:forRowAtIndexPath:
you can:
NSIndexPath *indexPathToSelect = ...
[self performSelector:@selector(selectRowAtIndexPath:)
withObject:indexPathToSelect
afterDelay:0];
Then define the selectRowAtIndexPath:
method in your view controller
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionTop];
}
When you delete a row, you should have its indexPath. Then you call the method
(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
For indexPath you can set the new indexPath, by increasing or decreasing the indexPath.row by 1.
scrollPosition sets whether the tableView scrolls up or down, and animated should be clear.
After all, you manually select that UITableViewCell.
[cell setSelected:YES];
精彩评论