准备工作
1.Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *phone;
@property(nonatomic,copy)NSString *address;
@property(nonatomic,copy)NSString *age;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *hobby;
@end
2.Student.m
#import "Student.h"
@implementation Student
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
3.RootViewController.m
#import "RootViewController.h"
#import "Student.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *stuArr;
@property(nonatomic,retain)UITableView *tableView;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor whiteColor];
[self createData];
self.tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
[self.tableView release];
self.tableView.dataSource=self;
self.tableView.delegate=self;
//设置导航栏编辑按钮
self.navigationItem.rightBarButtonItem=self.editButtonItem;
//开启一下tableView的编辑模式
// [self.tableView setEditing:YES animated:YES];
}
//重写一下系统提供的编辑按钮(rightButton)点击方法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
}
-(void)createData{
NSString *path=@"/Users/dllo/Desktop/UI/UI11_tableView编辑/UI11_tableView编辑/Student.plist";
NSArray *arr=[NSArray arrayWithContentsOfFile:path];
self.stuArr=[NSMutableArray array];
for (NSDictionary *dic in arr) {
Student *stu=[[Student alloc] init];
[stu setValuesForKeysWithDictionary:dic];
[self.stuArr addObject:stu];
[stu release];
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Student *stu=self.stuArr[indexPath.row];
stu.name=@"陈伟霆";
// [self.tableView reloadData];
//单行刷新(不消耗资源)
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.stuArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuse=@"reuse";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
Student *stu=self.stuArr[indexPath.row];
cell.textLabel.text=stu.name;
return cell;
}
二.
逐行的去设置哪些行编辑,哪些行不编辑
#pragma mark 逐行的去设置哪些行编辑,哪些行不编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.row%2==0) {
// return YES;
// }else{
return YES;
// }
}
设置编辑模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//返回的是多选状态
// return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleDelete;
}
设置删除按钮的标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"来点我";
}
实现左滑效果,而且是对应按钮功能的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"编辑开始");
//判断当前编辑模式
if(editingStyle==UITableViewCellEditingStyleDelete){
//先把数组里的对象删掉
[self.stuArr removeObjectAtIndex:indexPath.row];
//第一种方式
// [self.tableView reloadData];
//第二种方式
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//他只是视觉上移动,并没有改变数据的顺序,所以想要改变数组顺序,需要用代码实现
//获取要移动的数据
Student *stu=[self.stuArr[sourceIndexPath.row] retain];
//在数组里把这个对象移除掉
[self.stuArr removeObjectAtIndex:sourceIndexPath.row];
[self.stuArr insertObject:stu atIndex:destinationIndexPath.row];
[stu release];
}
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *actionFirst=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"通话" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"233333333333");
}];
actionFirst.backgroundColor=[UIColor cyanColor];
return @[actionFirst];
}