iOS學習之UITableView(二):進階篇列表中行的操作

關於列表的創建,上一篇博文已經介紹,本文直接對列表行進行操作,下一篇介紹幾種樣式的列表:索引,標記和自定義的table

一、選中行

    實現代理方法

// 選中行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"您點擊了第%d分區第%d行",indexPath.section, indexPath.row);
    
    // 取消選中狀態
    // [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

二、刪除行

    要對行進行操作,首先要實現代理方法

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

    先講述單獨刪除一行數據,即左滑出現刪除按鈕,並刪除行的操作,後文會介紹多選批量刪除

                              圖1 刪除行

          

       1. 可重置刪除按鈕的標題,默認爲"delete"

// 設置刪除按鈕標題
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}

       2. 點擊刪除後

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 從數據源中刪除
    [self.dataArray removeObjectAtIndex:indexPath.row];
    
    // 從列表中刪除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
   
}

三、插入行

     ①這時我將插入行和刪除行都以一個按鈕動作來觸發,點擊後tableView進入編輯模式,先上效果圖

                   圖2 進入添加行模式                                 圖3 進入刪除行模式

     

      ②在ViewDidLoad中添加代碼,其中self.addButton和self.deleteBarButtonItem均在storyBoard中創建,下文中的按鈕也是這種情況

NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
    
self.navigationItem.leftBarButtonItems = leftBarButtons;//設置導航欄左邊按鈕爲添加和刪除按鈕

      ③在@interface中聲明一個變量

UITableViewCellEditingStyle selectEditingStyle;

      ④兩個按鈕的點擊事件

// 更新導航欄按鈕
-(void) updateBarButtons
{
    if (self.tableView.editing==YES) {
        self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
    }
}
// 點擊添加按鈕
- (IBAction)addButtonClicked:(id)sender {
    selectEditingStyle = UITableViewCellEditingStyleInsert;
    
    [self.tableView setEditing:YES animated:YES];
    
    [self updateBarButtons];
    
}
// 點擊刪除按鈕
- (IBAction)deleteButtonClicked:(id)sender {
    
    
    selectEditingStyle = UITableViewCellEditingStyleDelete;
    
    [self.tableView setEditing:YES animated:YES];
    
    [self updateBarButtons];
}

      ⑤實現相應的代理方法

// 是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
// 編輯模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return selectEditingStyle;
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 刪除模式
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        
        // 從數據源中刪除
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 刪除行
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    // 添加模式
    else if(editingStyle == UITableViewCellEditingStyleInsert){
        
        // 從數據源中添加
        [self.dataArray insertObject:@"new iPhone" atIndex:indexPath.row];
        
        // 添加行
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic ];
    }
   
}
// 點擊完成按鈕
- (IBAction)doneButtonClicked:(id)sender {
    
    [self.tableView setEditing:NO animated:YES];
    
    [self updateBarButtons];
}


四、移動行

      ①效果圖

                                圖4 移動行

            

     ②在tableView進入編輯模式時,可以對行進行移動操作,通過方法

// 是否支持移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

     ③設置行可移動,並完成移動行方法,改變數據源

// 移動行操作-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{// 這裏其實就是數組中兩個變量交換位置的過程 id object = [self.dataArray objectAtIndex:fromIndexPath.row];
    
    [self.dataArray removeObjectAtIndex:fromIndexPath.row];
    
    [self.dataArray insertObject:object atIndex:toIndexPath.row];
}

五、批量刪除行

     ①即完成可以選擇多個行之後批量刪除,如圖

                    圖5 進入批量刪除模式                                  圖6 選中行準備刪除

       

      ②在ViewDidLoad中添加代碼

self.navigationItem.rightBarButtonItem = self.editBarButtonItem;// 在右導航欄中添加編輯按鈕

     ③現在需要達到,點擊編輯按鈕在右上角出現取消按鈕,左上角出現刪除按鈕。並在選擇時,能出現刪除行的數量,修改updateBarButtons方法,並添加一個方法來根據條件修改刪除按鈕的標題

// 更新導航欄按鈕
-(void) updateBarButtons
{
    // 如果是允許多選的狀態,即進入批量刪除模式
    if (self.tableView.allowsSelectionDuringEditing == YES) {
        //更新刪除按鈕
        [self updateDeleteButtonTitle];
        // 導航欄左邊按鈕設置爲空
        self.navigationItem.leftBarButtonItems = nil;
        // 將左邊按鈕設置爲'批量刪除'按鈕
        self.navigationItem.leftBarButtonItem = self.multiDeleteBarButton;
        // 導航欄右鍵設置爲'取消'鍵
        self.navigationItem.rightBarButtonItem = self.cancelBarButtonItem;
        
        return;
    }
    if (self.tableView.editing==YES) {// 如果是編輯狀態,且不屬於批量刪除狀態
        // 導航欄右鍵設置爲'取消'鍵
        self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
    }
    else {// 如果不是編輯狀態,將導航欄設置爲初始狀態的樣式,即左欄爲'添加','刪除'按鈕,右欄爲'編輯'按鈕
        NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
        self.navigationItem.leftBarButtonItems = leftBarButtons;
        
        self.navigationItem.rightBarButtonItem = self.editBarButtonItem;
    }
}


// 更新刪除按鈕的標題
-(void)updateDeleteButtonTitle
{
    NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];//得到選中行
    
    BOOL allItemsAreSelected = selectedRows.count == self.dataArray.count;// 是否全選
    
    BOOL noItemsAreSelected = selectedRows.count == 0;// 選中行數是否爲零
    
    if (allItemsAreSelected || noItemsAreSelected)
    {// 如果是全選或者未選,則刪除鍵爲刪除全部
        self.multiDeleteBarButton.title = @"刪除全部";
    }
    else
    {// 否則 刪除鍵爲刪除(選中行數量)
        self.multiDeleteBarButton.title = [NSString stringWithFormat:@"刪除 (%d)", selectedRows.count];
    }

}

④在

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

兩個方法中調用updateDeleteButtonTitle方法

⑤點擊編輯按鈕時

// 編輯按鈕
- (IBAction)editButtonClicked:(id)sender {  
    self.tableView.allowsMultipleSelectionDuringEditing = YES;// 進入可多選刪除狀態
    
    [self.tableView setEditing:YES animated:YES];// 將table設置爲可編輯
    
    [self updateBarButtons];  //更改導航欄的導航按鈕
}

    ⑥點擊刪除多個按鈕時

- (IBAction)multiDeleteClicked:(id)sender {
    // 選中的行
    NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
    
    // 是否刪除特定的行
    BOOL deleteSpecificRows = selectedRows.count > 0;
    // 刪除特定的行
    if (deleteSpecificRows)
    {
        // 將所選的行的索引值放在一個集合中進行批量刪除
        NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
        
        for (NSIndexPath *selectionIndex in selectedRows)
        {
            [indicesOfItemsToDelete addIndex:selectionIndex.row];
        }
        // 從數據源中刪除所選行對應的值
        [self.dataArray removeObjectsAtIndexes:indicesOfItemsToDelete];
        
        //刪除所選的行
        [self.tableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else
    {
        // 刪除全部
        [self.dataArray removeAllObjects];
        
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    // 刪除完成,退出編輯狀態,並退出多選狀態,同時更新導航欄的按鈕
    [self.tableView setEditing:NO animated:YES];
    
    self.tableView.allowsMultipleSelectionDuringEditing = NO;
    
    [self updateBarButtons];
}

代碼下載

http://www.oschina.net/action/code/download?code=33584&id=48435

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章