您的位置:首页 > 移动开发 > Objective-C

Objective--C UI UITableView编辑

2015-12-22 19:13 302 查看
self.tableView = [[UITableViewalloc]
initWithFrame:self.view.framestyle:UITableViewStylePlain];

self.tableView.delegate =self;

self.tableView.dataSource =self;

[self.viewaddSubview:self.tableView];

[_tableView
release];

// 设置导航栏编辑按钮

self.navigationItem.rightBarButtonItem =self.editButtonItem;

//
开启tableView的编辑模式

// [self.tableView setEditing:YES];

[selfcreateData];

}

- (void)createData{

// 找到要处理的数据的路径

NSString *path =@"/Users/dllo/Desktop/资料库/Student.plist";

// 创建一个数组接收

NSArray *stuArr = [NSArrayarrayWithContentsOfFile:path];

// 初始化定义好的属性数组

self.arr = [NSMutableArrayarray];

for (NSDictionary *dicin stuArr) {

Student *stu = [[Studentalloc]
init];

// Student是继承于NSObject的一对文件,在.h中写所有用到的属性,在.m中写

//-(void)setValue:(id)value forUndefinedKey:(NSString *)key方法,这个方法是KVC里负责纠错的方法,只要key和属性名没有对应上,就会执行这个方法.

// MVC : Model-View-Controller

// 将字典中的数值放到对象stu中,再想调用字典中的值时不用字典,而改用stu对象调用

[stu setValuesForKeysWithDictionary:dic];

//
把传好值的stu放到数组中

[self.arraddObject:stu];

[stu release];

}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

// tableView跟数组进行关联

return
self.arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *reuse =@"reuse";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:reuse];

if (!cell) {

cell = [[[UITableViewCellalloc]
initWithStyle:UITableViewCellStyleValue1reuseIdentifier:reuse]
autorelease];

}

// 找到想要进行编辑的对象

Student *stu =
self.arr[indexPath.row];

//
获取信息到tableView,将姓名显示在tableView上

cell.textLabel.text = stu.name;

return cell;

}

// 重写系统提供的编辑按钮的点击方法

// 编辑按钮(Edit)完成后,点击Done,编辑模式会消失.

// tableView的编辑跟随系统变化,可以省去前面写的开启编辑模式

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

[super setEditing:editinganimated:animated];

[self.tableViewsetEditing:editing
animated:animated];

}

#pragma mark 逐行设置,对哪些行进行编辑,对哪些行不编辑

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

//
实现多选效果

// return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;

returnUITableViewCellEditingStyleDelete;

}

#pragma mark 设置删除按钮的标题

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

return @"删除";

}

#pragma mark 实现左滑效果,而且是对应按钮功能的方法

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

// 先判断当前的编辑模式

if (editingStyle ==UITableViewCellEditingStyleDelete) {

//
先删除组里的对象

[self.arrremoveObjectAtIndex:indexPath.row];

//
刷新tableView

[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationBottom];

}

}

#pragma mark 拖拽移动

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

// 在这个方法中什么都不写运行的时候,会发现,按住右侧的横线也可以进行拖拽移动.但这只是视觉上的移动,并没有实际改变数组的顺序.因此,想要改变数组的顺序,需要用下面的代码来实现

// 找到想要进行移动的源对象

// retain -- release:防止内存隐患引起的崩溃

Student *stu = [self.arr[sourceIndexPath.row]retain];

// 在数组中把这个对象移除

[self.arrremoveObjectAtIndex:sourceIndexPath.row];

// 插入:将源对象放在目标行

[self.arrinsertObject:stu
atIndex:destinationIndexPath.row];

[stu release];

}

// 左滑显示"通话-消息"

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath
*)indexPath{

UITableViewRowAction *actionFirst = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"通话"handler:^(UITableViewRowAction
*_Nonnull action,NSIndexPath *
_Nonnull indexPath) {

}];

//
修改颜色,默认红色

actionFirst.backgroundColor = [UIColorcyanColor];

UITableViewRowAction *actionSecond = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"消息"handler:^(UITableViewRowAction
*_Nonnull action,NSIndexPath *
_Nonnull indexPath) {

}];

return @[actionFirst,actionSecond];

}

// 点击事件

// 实现点击改名字

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

// 找到点击修改的对象

Student *stu =
self.arr[indexPath.row];

// 给要修改的对象改名

stu.name = @"张三";

//
单行刷新:优化tableView,避免消耗资源

[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: