您的位置:首页 > 产品设计 > UI/UE

UITableView的编辑

2015-08-11 20:39 471 查看
效果图:



创建两个属性:tableView,和可变数组arr

1.对数组进行初始化

代码:

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr= [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}


2.在viewDidLoad中创建self.tableView

代码:

self.view.backgroundColor=[UIColor yellowColor];
// 取消透明度  self.navigationController.navigationBar.translucent=NO;
self.tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
[self.tableView release];
self.tableView.delegate =self;
self.tableView.dataSource=self;
// 设定行高
self.tableView.rowHeight=100;
// 创建右面的按钮  self.navigationItem.rightBarButtonItem=self.editButtonItem;


3.下面的方法可以直接打开tableView的可编辑模式(一般情况下不会直接开启这种方法,都会用杆控制这种方法的开启)

[self.tableView setEditing:YES animated:YES];


4,重写系统的编辑按钮点击触发的方法

代码:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
//调用父类方法
[super setEditing:editing animated:animated];
// 用edit按钮来控制编辑的触发
[self.tableView setEditing:editing animated:YES];
}


5. 设置哪行可以进行编辑

代码:

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
// 奇数行可以编辑,偶数行不可以编辑
if(indexPath.row%2){
return YES;
}else{
return NO;
}
}


6.设计编辑时的样式

代码:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}


7. 删除数据 (添加左滑删除)

代码

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"恭喜" message:@"删除成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
if (editingStyle ==UITableViewCellEditingStyleDelete) {
// 先删除数据源
[self.arr removeObject:self.arr[indexPath.row]];
//        [self.tableView reloadData];
// 通过tableView来删除上面的cell
// 第一个参数:指定删除哪一个分区的哪一行.把它作为一个元素放在数组中
// 第二个元素:删除动画
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]
}
}


8. 修改删除按钮的标题

代码:

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"来点我啊";
}


9.这个方法是iOS8.0之后出现的方法,可以在编辑状态的时候有多个按钮

代码:

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *deleteAction=[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// 按钮的点击所要触发的事件,都是写在block
NSLog(@"触发了删除按钮");
// 删除点击下标的所对的数组中的元素
[self.arr removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}];
deleteAction.backgroundColor =[UIColor cyanColor];

UITableViewRowAction *topAction =[UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
(1).先获取到起始位置的数据
NSString *str =[self.arr[indexPath.row] retain];
(2).把起始位置的对象从数据源中移除(删除需要先retain 再插入之后 在最后release)
[self.arr removeObjectAtIndex:indexPath.row];
(3).把数据插入到数组的目的位置上去
[self.arr insertObject:str atIndex:0];
[str release];
(4).刷新self.tableView
[self.tableView reloadData];
}];
return @[deleteAction,topAction];
}


10 .对cell的创建,(第二个必须执行的协议)

代码:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.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];
}
cell.textLabel.text=self.arr[indexPath.row];
return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息