iOS学习笔记

UITableView代理方法简介

2017-05-17  本文已影响7人  PZcoder

目录:
1、根据条件显示tableview是否在编辑中
2、设置一共有几个分区
3、设置要展示的行数
4、设置每行要展示的数据
5、改变每行的高度方法
6、点击cell时调用方法
7、 完成点击(放弃选中)操作(此方法不常用)
8、设置表视图可以编辑
9、指定表视图的单元格如何编辑
10、结束编辑时回调方法
11、指定表视图出现右侧移动按钮
12、实现移动的方法
13、改变删除按钮字样
14、自定义左滑按钮

1、根据条件显示tableview是否在编辑中

-(void)doEditing:(UIBarButtonItem *)sender
{
    //根据sender的title做出相应的处理
    if ([sender.title isEqualToString:@"编辑"]) {
        sender.title = @"完成";
        self.tableView.editing = YES;
    }else if ([sender.title isEqualToString:@"完成"]){
        self.tableView.editing = NO;
        sender.title = @"编辑";
    }
}

2、设置一共有几个分区

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

3、设置要展示的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.marr.count;
}

4、设置每行要展示的数据

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Person * per = self.marr[indexPath.row];
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([Person class]) forIndexPath:indexPath];
    [cell setCells:per];
    return cell;
}

5、改变每行的高度方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

6、点击cell是调用此方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //可以完成视图跳转
    //根据当前点击的行索引,获取当前显示的对象
//    Person *per = self.marr[indexPath.row];
//    NSLog(@"�选中%@,电话是%@",per.name,per.tel);
    //隐藏完成点击后的效果
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

7、 完成点击(放弃选中)操作(此方法不常用)

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击完成。");
}

8、设置表视图可以编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

9、指定表视图的单元格如何编辑

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return UITableViewCellEditingStyleInsert;
    }else{
        return UITableViewCellEditingStyleDelete;
    }
}

10、 结束编辑时回调此方法

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //执行删除操作时先从数据源删除
        [self.marr removeObjectAtIndex:indexPath.row];
        //刷新表视图
//        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }else if (editingStyle == UITableViewCellEditingStyleInsert){
        Person *per = self.marr[indexPath.row];
        //首先为数据源中对应行插入数据然后刷新
        [self.marr insertObject:per atIndex:indexPath.row];
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

11、指定表视图出现右侧移动按钮

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

12、实现移动的方法

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //获取移动的行索引
    NSInteger from = sourceIndexPath.row;
    Person *per = self.marr[from];
    NSInteger to = destinationIndexPath.row;
    //先删除要移动的对象,再插入到相应位置上
    [self.marr removeObject:self.marr[from]];
    [self.marr insertObject:per atIndex:to];
}

13、改变删除按钮字样

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

14、自定义左滑按钮

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        Person *per = self.marr[indexPath.row];//获取当前行显示对象
        [self.marr removeObject:self.marr[indexPath.row]];//移除当前行的对象
        [self.marr insertObject:per atIndex:0];//添加到第一行
        [self.tableView reloadData];//刷新表视图
    }];
    action1.backgroundColor = [UIColor orangeColor];//设置背景颜色
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
   {
        [self.marr removeObject:self.marr[indexPath.row]];
        [self.tableView reloadData];
    }];
    
    UITableViewRowAction *action3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"标为未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) 
    {
        //获取当前单元格
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        //改变扩展按钮
        cell.accessoryType = UITableViewCellAccessoryCheckmark;//自定义标记
        [self.tableView reloadData];
    }];
    action3.backgroundColor = [UIColor grayColor];
    
    return @[action1,action2,action3];//返回数组类型
}
上一篇下一篇

猜你喜欢

热点阅读