局部刷新方法
添加數(shù)據(jù)
NSArray *indexPaths = @[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
刪除數(shù)據(jù)
NSArray *indexPaths = @[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
更新數(shù)據(jù)(沒有添加和刪除數(shù)據(jù)酱塔,僅僅是修改已經(jīng)存在的數(shù)據(jù))
NSArray *indexPaths = @[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
左滑出現(xiàn)刪除按鈕
需要實現(xiàn)tableView的代理方法
// 只要實現(xiàn)了這個方法,左滑出現(xiàn)Delete按鈕的功能就有了
// 點擊了“左滑出現(xiàn)的Delete按鈕”會調(diào)用這個方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 刪除模型
[self.wineArray removeObjectAtIndex:indexPath.row];
// 刷新
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
// 修改Delete按鈕文字為“刪除”
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除";
}
左滑出現(xiàn)N個按鈕
需要實現(xiàn)tableView的代理方法只要實現(xiàn)了這個方法弟蚀,左滑出現(xiàn)按鈕的功能就有了(一旦左滑出現(xiàn)了N個按鈕拌消,tableView就進入了編輯模式, tableView.editing = YES)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
// 左滑cell時出現(xiàn)什么按鈕
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關(guān)注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"點擊了關(guān)注");
// 收回左滑出現(xiàn)的按鈕(退出編輯模式)
tableView.editing = NO;
}];
UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[self.wineArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];
return @[action1, action0];
}
進入編輯模式
// self.tabelView.editing = YES;
[self.tableView setEditing:YES animated:YES];
// 默認情況下挑豌,進入編輯模式時,左邊會出現(xiàn)一排紅色的“減號”按鈕
在編輯模式中多選
// 編輯模式的時候可以多選
self.tableView.allowsMultipleSelectionDuringEditing = YES;
// 進入編輯模式
[self.tableView setEditing:YES animated:YES];
// 獲得選中的所有行
self.tableView.indexPathsForSelectedRows;