IOS Cell 的删除 增加 移动操作

//当前行是否可以编辑

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

if(indexPath.row==0)returnNO;

returnYES;

}

//当前行是如何编辑(删除增加)

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

//最后一行插入

if(indexPath.row==self.mArray.count-1)

returnUITableViewCellEditingStyleInsert;

//其他行删除

returnUITableViewCellEditingStyleDelete;

}

//编辑后(删除或增加)如何处理

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

if(editingStyle ==UITableViewCellEditingStyleDelete) {

NSLog(@"删除元素");

//先把数组中的元素删除

[self.mArrayremoveObjectAtIndex:indexPath.row];

[tableViewreloadData];

//[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}elseif(editingStyle ==UITableViewCellEditingStyleInsert) {

NSLog(@"插入元素");

//先向数组中插入元素

[self.mArrayaddObject:@"赵六"];

//[tableView reloadData];

NSIndexPath*myIndexPath = [NSIndexPathindexPathForRow:self.mArray.count-1inSection:0];

[tableViewinsertRowsAtIndexPaths:@[myIndexPath]withRowAnimation:UITableViewRowAnimationLeft];

NSLog(@"--- %@",self.mArray);

}

}

//当前行是否可以移动

-(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(nonnullNSIndexPath*)indexPath {

returnYES;

}

//移动完成后如何操作

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

//移动完成后数组里的元素也应该交换位置

NSString*name =self.mArray[sourceIndexPath.row];

//从数组中删除

[self.mArrayremoveObjectAtIndex:sourceIndexPath.row];

//把元素插入到目标的位置

[self.mArrayinsertObject:nameatIndex:destinationIndexPath.row];

[tableViewreloadData];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容