UITableView的编辑

效果图:
这里写图片描述

创建两个属性: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值