cell 滑动 --> UITableViewRowAction

iOS 8之前使用如下方式自定义UITableView左划后显示的文字,不过该样式太单一了,而且只能显示一个:

- (nullable NSString *)tableView:(UITableView *)tableView 
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

iOS8之后,只需要一个tableView代理方tableView:editActionsForRowAtIndexPath:和一个类UITableViewRowAction就可以了。 rowAction可以设置style、title、backgroundColor、backgroundEffect,在block中实现点击事件

注意 : title 可以设置,但是title的文字颜色没办法设置。
其中不修改backgroundColor时,backgroundColor的颜色是由style决定的.

  • UITableViewRowActionStyleDestructive时是红色的删除样式,
  • UITableViewRowActionStyleNormal时是灰色样式,类似于微信好友列表左划后的“备注”。
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

当实现该代理方法后,以下的这个代理方法就不执行了:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaul ttitle:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        [self.arrremoveObjectAtIndex:indexPath.row];
       [self.tableViewdeleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }];
    
    //设置收藏按钮
    UITableViewRowAction *collectRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"收藏"message:@"收藏成功"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
        [alertView show];
    }];
    //设置置顶按钮
    UITableViewRowAction *topRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaul ttitle:@"置顶"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
        [self.arrinsertObject:self.arr[indexPath.row]atIndex:0];
        [self.arrremoveObjectAtIndex:indexPath.row +1];
        NSIndexSet *set = [NSIndexSetindexSetWithIndex:0];
        [tableView reloadSections:setwithRowAnimation:UITableViewRowAnimationTop];
        /**
         *  tableView 刷新时如果确定哪个row 或者section就刷新对应的,不要走reloadData
         */
    }];

    topRowAction.backgroundColor = [UIColorblueColor];
    collectRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
    return  @[deleteRowAction,collectRowAction,topRowAction];
}

参考 :https://blog.csdn.net/mlcldh/article/details/54947302
https://blog.csdn.net/textfielddelegate/article/details/50599151
https://www.cnblogs.com/zj901203/p/4606804.html
https://blog.csdn.net/zhanglizhi111/article/details/52922915

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 11,247评论 12 197
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,569评论 25 708
  • 远房侄子上一年级,昨天到家里来玩。说到期末考试成绩。“考的咋样?”“我数学考的不好。”“多少分?”他有点不好意思的...
    墨行川阅读 448评论 0 0
  • 何事惹人愁,薄凉似深秋。听叶落,瑟瑟到心头。残花飞过溪边柳,声声问,盼可留。 丝雨落轻舟,两岸...
    请叫我想念熊阅读 218评论 3 2
  • 前世的五百次回眸才能换得今生的一次擦肩而过,那么,我要用多少次回眸才能真正住进你的心中?(——席慕蓉) 人的...
    阿John阅读 225评论 0 1