UITableView的編輯狀態(tài)

一、cell編輯步驟

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

2、設(shè)置某些cell 可以編輯

3、設(shè)置某些cell的編輯樣式

4、處理編輯結(jié)果

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

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

{

[super setEditing:editing animated:animated];

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

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

}

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

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

//設(shè)置編輯樣式

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

{

/*

Delete:刪除

Insert:添加

Delete | Insert:多選

*/

return UITableViewCellEditingStyleInsert;

}

//處理編輯結(jié)果

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

{

//處理刪除結(jié)果

if (editingStyle == UITableViewCellEditingStyleDelete) {

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

[self.dataArray removeObjectAtIndex:indexPath.row];

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

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

//簡(jiǎn)單粗暴 無(wú)動(dòng)畫(huà),效率低

[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];

//簡(jiǎn)單粗暴 無(wú)動(dòng)畫(huà),效率低

[self.tableView reloadData];

}

//設(shè)置是否可以移動(dòng)

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

//處理移動(dòng)結(jié)果

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

sourceIndexPath.row:那一行

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

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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