UITableView的編輯狀態(tài)

一、cell編輯步驟

1、讓tableView 處于編輯狀態(tài)

2、設置某些cell 可以編輯

3、設置某些cell的編輯樣式

4、處理編輯結果

//self.editButtonItem響應方法,讓tableView 處于編輯狀態(tài)

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

{

[super setEditing:editing animated:animated];

//開啟或關閉tableView的編輯狀態(tài)

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

}

//返回YES,則表示該cell可以被編輯,返回NO,則表示該cell不可以被編輯

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

//設置編輯樣式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

/*

Delete:刪除

Insert:添加

Delete | Insert:多選

*/

return UITableViewCellEditingStyleInsert;

}

//處理編輯結果

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

{

//處理刪除結果

if (editingStyle == UITableViewCellEditingStyleDelete) {

//第一、刪除數(shù)據(jù)源數(shù)據(jù)

[self.dataArray removeObjectAtIndex:indexPath.row];

//第二、刪除cell或者更新tableView

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

//簡單粗暴 無動畫,效率低

[self.tableView reloadData];

}

else if (editingStyle == UITableViewCellEditingStyleInsert)

{

//第一步、添加數(shù)據(jù)源數(shù)據(jù)

Contact *con = self.dataArray[indexPath.row];

[self.dataArray insertObject:con atIndex:indexPath.row+1];

//第二步、添加cell或者更新tableView

NSIndexPath *myPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

[self.tableView insertRowsAtIndexPaths:@[myPath] withRowAnimation:UITableViewRowAnimationBottom];

//簡單粗暴 無動畫,效率低

[self.tableView reloadData];

}

//設置是否可以移動

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

//處理移動結果

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

sourceIndexPath.row:那一行

destinationIndexPath.row:目標行數(shù)

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

推薦閱讀更多精彩內容