与TableView插入、删除、移动、多选,刷新控件

一、插入、删除、移动、多选

方法一:

Cell的插入、删除、移动都有一个通用的方法,就是更新tableView的数据源,再reloadData,这样做实现上是简单一点,但是reloadData是刷新整个tableView,消耗性能。

方法二:

针对指定的位置进行插入、删除、移动

步骤:

1.让tableView进入编辑状态:

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

2.tableView会执行代理返回编辑的种类:

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

  插入:UITableViewCellEditingStyleInsert,左侧出现红色删除按钮

  删除:UITableViewCellEditingStyleDelete,左侧出现绿色添加按钮

  移动:UITableViewCellEditingStyleNone,右侧出现灰色移动按钮

  多选:UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert,左侧出现圆形选择框

3.在插入、删除、多选的时候右侧的移动按钮也会出现,这是因为tableView的代理- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath默认返回YES,所以默认编辑的时候可以移动,不需要的设置成NO就行了

4.执行tableView实现插入、删除、移动、多选

  插入:- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

  删除:- (void)deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

  移动:不需要实现方法,移动后会执行回调- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

  多选:不需要什么实现方法,cell的selectionStyle不能为UITableViewCellSelectionStyleNone不然没有多选效果,选中的一个cell的时候会执行代理- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath ,取消选中一个cell的时候执行回调:- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

 

二、系统刷新控件

在TableViewController中可以使用系统的刷新控件UIRefreshControl

使用方法:

1.创建UIRefreshControl

  //初始化

  UIRefreshControl *rc = [[UIRefreshControl alloc] init];

  //设置Title

  rc.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];

  //响应方法

  [rc addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];

  //赋给TableViewController

      self.refreshControl = rc;

 

- (void)refresh{

  //控件状态,是否在刷新状态

        if (self.refreshControl.isRefreshing) {

                self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"加载中"];

    }

}

  

 

 

原文地址:https://www.cnblogs.com/small-octopus/p/4886543.html