1.首先设置可以编辑
[self.tableView setEditing:YES animated:YES];
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//对数据源进行重新排序
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
[models exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
//如果是删除和添加的话,不过需要在style中返回要使用的cell的style
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleInsert) {
//添加操作
[models insertObject:@"xxgxgxu" atIndex:indexPath.row];
}
else if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除操作
[models removeObjectAtIndex:indexPath.row];
}
[self.tableView reloadData];
}
本文介绍了如何在iOS应用中实现TableView的拖动重排功能。通过设置TableView为可编辑模式,实现UITableViewCellEditingStyleInsert样式,并重写canMoveRowAtIndexPath方法允许单元格移动。在moveRowAtIndexPath中更新数据源,完成移动操作。同时,提供了处理删除和添加行的方法。
15

被折叠的 条评论
为什么被折叠?



