zoukankan      html  css  js  c++  java
  • ios cell左滑删除

    iOS项目开发小技能 (三) -UITableView实现Cell左划删除等自定义功能

    www.MyException.Cn  网友分享于:2015-06-05  浏览:0次
     
    iOS项目开发小技巧 (三) --UITableView实现Cell左划删除等自定义功能

    今天来介绍下iOS开发中UITableView的Cell左划实现微信中置顶,删除等功能。该功能在iOS8.0以前是需要很复杂的实现,不过github上应该有现成demo,不过今天介绍的是在iOS8.0以后苹果新推出的api,来实现Cell左划自定义控件。 
    1. 首先创建UITableView视图,实现其俩个代理,UITableViewDelegate和UITableViewDataSource,该处代码就不说了,主要是俩个回调方法 
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    2. 然后实现另一个代理方法 
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

    editingStyle = UITableViewCellEditingStyleDelete;//此处的EditingStyle可等于任意UITableViewCellEditingStyle,该行代码只在iOS8.0以前版本有作用,也可以不实现。 


    3. 再实现 
    `-(NSArray )tableView:(UITableView )tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 

    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@”删除” handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {//title可自已定义 
    NSLog(@”点击删除”); 
    }];//此处是iOS8.0以后苹果最新推出的api,UITableViewRowAction,Style是划出的标签颜色等状态的定义,这里也可自行定义

    UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
    
    }];
    editRowAction.backgroundColor = [UIColor colorWithRed:0 green:124/255.0 blue:223/255.0 alpha:1];//可以定义RowAction的颜色
     return @[deleteRoWAction, editRowAction];//最后返回这俩个RowAction 的数组
    

    }` 
    这样就实现了如下的效果

  • 相关阅读:
    必读:Spark与kafka010整合
    6 个开源的家庭自己主动化工具 | Linux 中国
    承上 DBlink 与 SCN | 新增视图找出外部 SCN 跳变
    NetBeans使用Consolas中文乱码的解决
    IDEA community + Gradle + Gretty 调试 servlet 应用 + war包部署到tomcat
    解决ubuntu下IntelliJ IDEA无法锁定到启动器的问题
    virtualbox安装android6.0并设置分辨率为1920x1080x32
    Ubuntu安装Sqlite报错:No module named 'ConfigParser'
    Linux常用指令笔记
    解决Warning Couldn't flush user prefs: java.util.prefs.BackingStoreException: Couldn't get file lock.
  • 原文地址:https://www.cnblogs.com/piaojin/p/5442539.html
Copyright © 2011-2022 走看看