zoukankan      html  css  js  c++  java
  • 与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:@"加载中"];

        }

    }

      

     

     

  • 相关阅读:
    HDU-1215 七夕节 数论 唯一分解定理 求约数之和
    LightOJ-1259 Goldbach`s Conjecture 数论 素数筛
    [前端-动态数据可视化]横向柱状图的动态数据可视化
    CodeForces-722C Destroying Array 并查集 离线操作
    CodeForces-920E Connected Components? 广度搜索 双向链表 判断联通 大量重复节点的删除
    CodeForces-1007A Reorder the Array 贪心 田忌赛马
    POJ-3692 Kindergarten 二分图 最大团
    个人开发者做一款Android App需要知道的事情
    android各种组件的监听器
    留言处插入xss不弹框
  • 原文地址:https://www.cnblogs.com/small-octopus/p/4886543.html
Copyright © 2011-2022 走看看