iOS學習之 Cell 數據刷新(全局和局部)

數據刷新

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

    • [self.tableView reloadData]; 屏幕上的所有可視的cell都會刷新一遍
  • 局部刷新方法

    1. 添加數據
    NSArray *indexPaths = @[
                              [NSIndexPath indexPathForRow:0 inSection:0],
                              [NSIndexPath indexPathForRow:1 inSection:0]
                              ];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    
    1. 刪除數據
    NSArray *indexPaths = @[
                            [NSIndexPath indexPathForRow:0 inSection:0],
                            [NSIndexPath indexPathForRow:1 inSection:0]
                            ];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
    
    1. 更新數據(沒有添加和刪除數據,僅僅是修改已經存在的數據)
    NSArray *indexPaths = @[
                            [NSIndexPath indexPathForRow:0 inSection:0],
                            [NSIndexPath indexPathForRow:1 inSection:0]
                            ];
    [self.tableView relaodRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
    
  • 左滑出現刪除按鈕

    • 需要實現tableView的代理方法
        // 只要實現了這個方法,左滑出現Delete按鈕的功能就有了
        // 點擊了“左滑出現的Delete按鈕”會調用這個方法
        - (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 @"刪除";
            }
            ```
    
    
  • 左滑出現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
        {
            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;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容