您的位置:首页 > 移动开发 > IOS开发

iOS 开发之如何编辑tableView上的cell

2015-09-15 17:23 537 查看
效果图展示:





plist 文件读取和调用
path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"DetailData.plist"];

list = [[NSArray arrayWithContentsOfFile:path]mutableCopy];

1、添加导航控制器,在导航控制器上添加两个 navigationItem 编辑 和 添加
2、点击编辑的时候调用方法
1⃣️//点击一次开始编辑,点击两次结束编辑
- (void)edit
{
[myTableView setEditing:!myTableView.isEditing animated:YES];
}
2⃣️编辑tableView
上的cell
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

//
判断编辑中的样式 是否是
删除的样式
if (editingStyle == UITableViewCellEditingStyleDelete ) {

// 1、删除数据
// 2、更新视图

//
移除数组里面的数据
[list removeObjectAtIndex:indexPath.row];
//
把移除后的数据 同步到 plist
里面
BOOL success = [list writeToFile:path atomically:YES];
//
如果数据同步成功 ,就删除数据里面的cell
if (success) {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}
}
}
3⃣️、cell之间的移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//
如果移动的不是同一个位置 才进行操作
if (sourceIndexPath.row != destinationIndexPath.row) {

// 1、
保存
需要移动的数据
NSDictionary *info = list[sourceIndexPath.row];
// 2、移除
需要移动的数据
[list removeObjectAtIndex:sourceIndexPath.row];
// 3、插入数据
// destinationIndexPath
可以得到 咱们要移动的位置
[list insertObject:info atIndex:destinationIndexPath.row];
// 4、同步数据到plist
[list writeToFile:path atomically:YES];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: