iOS - UITableView 編輯(cell的插入, 刪除, 移動)

來源:互聯網
上載者:User

標籤:index   script   tin   wan   插入   represent   from   功能   nba   

UITableView Cell的插入/刪除

核心API

Class : UITableView
Delegate : UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方詳細注釋詳見本章結尾)

/** TableView 進入或退出編輯狀態(TableView 方法). */- (void)setEditing:(BOOL)editing animated:(BOOL)animate/** 確定哪些行的cell可以編輯 (UITableViewDataSource協議中方法). */- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath/** 設定某一行cell的編輯模式 (UITableViewDelegate協議中方法). */TableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath/** 提交編輯狀態 (UITableViewDataSource協議中方法). */- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath/** 插入 cell (UITableView 方法). */- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation/** 刪除 cell (UITableView 方法). */- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation


功能實現
思路:

1.讓TableView 進入編輯狀態2.指定哪些 cell 可以進行編輯3.指定cell的編輯狀態(刪除還是插入)4.選中刪除(或插入)狀態之後的操作(資料來源進行更新, cell刪除或插入)

 Code:
1 . 讓TableView 進入編輯狀態 (UIViewControll.m)

/** 當點擊UINavigationBar 上面系統提供的編輯按鈕的時候, 系統會調用這個方法. */- (void)setEditing:(BOOL)editing animated:(BOOL)animated{    /** 首先調用父類的方法. */    [super setEditing:editing animated:animated];    /** 使tableView處於編輯狀態. */    [self.tableView setEditing:editing animated:animated];}

2. 指定哪些行的 cell 可以進行編輯 (UITableViewDataSource 協議方法)

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    if (0 == indexPath.row) {        return NO;  /**< 第一行不能進行編輯. */    } else {         return YES;    }}

3.指定cell的編輯狀態(刪除還是插入) (UITableViewDelegate 協議方法)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    /** 不同的行, 可以設定不同的編輯樣式, 編輯樣式是一個枚舉類型 */    if (indexPath.row == 0) {        return UITableViewCellEditingStyleInsert;     } else {        return UITableViewCellEditingStyleDelete;    } }

4.選中刪除(或插入)狀態之後的操作(資料來源進行更新, cell刪除或插入) (UITableViewDataSource 協議方法)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    /**   點擊 刪除 按鈕的操作 */    if (editingStyle == UITableViewCellEditingStyleDelete) { /**< 判斷編輯狀態是刪除時. */        /** 1. 更新資料來源(數組): 根據indexPaht.row作為數組下標, 從數組中刪除資料. */        [self.arr removeObjectAtIndex:indexPath.row];         /** 2. TableView中 刪除一個cell. */        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];    }    /** 點擊 +號 表徵圖的操作. */    if (editingStyle == UITableViewCellEditingStyleInsert) { /**< 判斷編輯狀態是插入時. */                /** 1. 更新資料來源:向數組中添加資料. */        [self.arr insertObject:@"abcd" atIndex:indexPath.row];        /** 2. TableView中插入一個cell. */        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];    }}

UITableView cell 的移動

核心API

Class: UITableView
Deletage: UITableViewDataSource, UITableViewDelegate
涉及的API:(API的官方詳細注釋詳見本章結尾)

/** TableView 進入或退出編輯狀態(TableView 方法). */- (void)setEditing:(BOOL)editing animated:(BOOL)animate/** 指定 tableView 哪些行(cell) 可以移動. */- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath/** 移動 cell. */- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

功能實現

思路:

1.讓 TableView 進入或退出 編輯狀態2.指定 tableView 哪些行(cell) 可以移動3.移動 cell 後的操作: 資料來源進行更新 
Code

1 . 讓 TableView 進入或退出 編輯狀態

/** 當點擊UINavigationBar 上面系統提供的編輯按鈕的時候, 系統會調用這個方法. */- (void)setEditing:(BOOL)editing animated:(BOOL)animated{    /** 首先調用父類的方法. */    [super setEditing:editing animated:animated];    /** 使tableView處於編輯狀態. */    [self.tableView setEditing:editing animated:animated];}

2.指定 tableView 哪些行(cell) 可以移動 (UITableViewDataSource協議方法)

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    /** 指定哪些行(cell)可以移動 */    if (0 == indexPath.row) {        return NO;  /**< NO cell不能移動 */    } else {        return YES; /**< YES cell可以移動 */    }}

3.移動 cell 後的操作: 資料來源進行更新

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    /**  1. 從原位置移除,在從原位置移除之前, 需要儲存一下原位置的資料, 同時持有一次. */    NSString *str = [[self.arr objectAtIndex:sourceIndexPath.row] retain];    [self.arr removeObjectAtIndex:sourceIndexPath.row];    /** 2. 添加到目的位置, 同時釋放一次 */    [self.arr insertObject:str atIndex:destinationIndexPath.row];    [str release];}

 API 官方注釋

/** * @brief   Asks the data source to commit the insertion or deletion of a specified row in the receiver. * * @param   <tableView>     The table-view object requesting the insertion or deletion. * @param   <editingStyle>  The cell editing style corresponding to a insertion or deletion requested for the row specified by indexPath. Possible editing styles are UITableViewCellEditingStyleInsert or UITableViewCellEditingStyleDelete. * @param   <indexPath>     An index path locating the row in tableView. */- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
/** * @brief   Asks the data source to verify that the given row is editable. * * @param   <tableView>     The table-view object requesting this information. * @param   <indexPath>     An index path locating a row in tableView. * * @return  YES if the row indicated by indexPath is editable; otherwise, NO. */- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
/** * @brief   Inserts rows in the table view at the locations identified by an array of index paths, with an option to animate the insertion. * * @param   <indexPaths>    An array of NSIndexPath objects, each representing a row index and section index that together identify a row in the table view. * @param   <animation>     A constant that either specifies the kind of animation to perform when inserting the cell or requests no animation. See Table Cell Insertion and Deletion Animation for descriptions of the constants. * **/- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
/** * @brief   Deletes the rows specified by an array of index paths, with an option to animate the deletion. *  * @param   <indexPaths>    An array of NSIndexPath objects identifying the rows to delete. * @param   <animation>     A constant that indicates how the deletion is to be animated, for example, fade out or slide out from the bottom. See Table Cell Insertion and Deletion Animation for descriptions of these constants. * */- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

 

iOS - UITableView 編輯(cell的插入, 刪除, 移動)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.