0701UITableView數據刷新

今天老師講了UITableView的數據刷新,以及UITableView的編輯刪除模塊。

借鑒老師的筆記并加入一些自己的理解。

## 全局刷新方法(最常用)

[self.tableView reloadData];// 沒什么好說的,通用型刷新方法。UITableView可視范圍都可刷新,缺點是略微會消耗一些性能,不能控制具體刷新的行數組數。有時候只需要刷新一行,結果調用全局刷新刷新了n行。這個時候就可以使用局部刷新。


## 局部刷新方法

- 添加數據

// 創建NSIndexPath數組對象,并添加數據。Row代表行Section代表組

NSArray *indexPaths = @[

[NSIndexPath indexPathForRow:0 inSection:0],

[NSIndexPath indexPathForRow:1 inSection:0]

];

// 如果添加后的行數和self.tabieview對應模型數組數對不上則會報錯。所以在添加的時候模型數組也需要做對應的數據添加。

[self.wineArray insertObjects:(NSArray *) atIndexes:(NSIndexSet *)]; // 如果想要將數據添加在表格最上面,則將新的數據插入至wineArray數組的小表0位置 。

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];


- 刪除數據

// 創建NSIndexPath數組對象,并刪除數據。Row代表行Section代表組

NSArray *indexPaths = @[

[NSIndexPath indexPathForRow:0 inSection:0],

[NSIndexPath indexPathForRow:1 inSection:0]

];

// 同樣如果刪除后的行數和self.tabieview對應模型數組對不上則會報錯。所以在刪除的時候要模型數組也需要做對應的數據刪除。

[self.wineArray removeObjectsInArray:(NSArray *)];

[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];

- 更新數據(沒有添加和刪除數據,僅僅是修改已經存在的數據)

NSArray *indexPaths = @[

[NSIndexPath indexPathForRow:0 inSection:0],

[NSIndexPath indexPathForRow:1 inSection:0]

];

[self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];

## 左滑出現刪除按鈕

- 需要實現tableView的代理方法

```objc

/**

*? 只要實現了這個方法,左滑出現Delete按鈕的功能就有了

*? 點擊了“左滑出現的Delete按鈕”會調用這個方法

*/

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

// 刪除模型

[self.wineArray removeObjectAtIndex:indexPath.row];

// 刷新UITableView并添加左滑動畫

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}

/**

*? 修改Delete按鈕文字為“刪除”

*/

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

return @"刪除";

}


## 左滑出現N個按鈕

- 需要實現tableView的代理方法

/**

*? 只要實現了這個方法,左滑出現按鈕的功能就有了

(一旦左滑出現了N個按鈕,tableView就進入了編輯模式, tableView.editing = YES)

*/

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

}

/**

*? 左滑cell時出現什么按鈕

*/

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

// 創建兩個滑動后的按鈕對象,設置名稱和樣式并綁定點擊觸發block

UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"關注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

NSLog(@"點擊了關注");

// 收回左滑出現的按鈕(退出編輯模式)

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];

// 默認情況下,進入編輯模式時,左邊會出現一排紅色的“減號”按鈕

## 在編輯模式中多選

// 編輯模式的時候可以多選

self.tableView.allowsMultipleSelectionDuringEditing = YES;

// 進入編輯模式

[self.tableView setEditing:YES animated:YES];

// 獲得選中的所有行

self.tableView.indexPathsForSelectedRows;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容