數(shù)據(jù)刷新
-
全局刷新方法(最常用)
-
[self.tableView reloadData];屏幕上的所有可視的cell都會(huì)刷新一遍
-
-
局部刷新方法
- 添加數(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)刪除按鈕
- 需要實(shí)現(xiàn)tableView的代理方法
// 只要實(shí)現(xiàn)了這個(gè)方法,左滑出現(xiàn)Delete按鈕的功能就有了 // 點(diǎn)擊了“左滑出現(xiàn)的Delete按鈕”會(huì)調(diào)用這個(gè)方法 - (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個(gè)按鈕
- 需要實(shí)現(xiàn)tableView的代理方法只要實(shí)現(xiàn)了這個(gè)方法,左滑出現(xiàn)按鈕的功能就有了(一旦左滑出現(xiàn)了N個(gè)按鈕,tableView就進(jìn)入了編輯模式, tableView.editing = YES)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { } // 左滑cell時(shí)出現(xiàn)什么按鈕 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關(guān)注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"點(diǎn)擊了關(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]; } 進(jìn)入編輯模式
// self.tabelView.editing = YES;
[self.tableView setEditing:YES animated:YES];
// 默認(rèn)情況下,進(jìn)入編輯模式時(shí),左邊會(huì)出現(xiàn)一排紅色的“減號(hào)”按鈕
- 在編輯模式中多選
// 編輯模式的時(shí)候可以多選
self.tableView.allowsMultipleSelectionDuringEditing = YES;
// 進(jìn)入編輯模式
[self.tableView setEditing:YES animated:YES];
// 獲得選中的所有行
self.tableView.indexPathsForSelectedRows;