iOS學(xué)習(xí)之 Cell 數(shù)據(jù)刷新(全局和局部)

數(shù)據(jù)刷新

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

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

    1. 添加數(shù)據(jù)
    NSArray *indexPaths = @[
                              [NSIndexPath indexPathForRow:0 inSection:0],
                              [NSIndexPath indexPathForRow:1 inSection:0]
                              ];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    
    1. 刪除數(shù)據(jù)
    NSArray *indexPaths = @[
                            [NSIndexPath indexPathForRow:0 inSection:0],
                            [NSIndexPath indexPathForRow:1 inSection:0]
                            ];
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
    
    1. 更新數(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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容