美文网首页
iOS 自定义cell多个删除按钮样式

iOS 自定义cell多个删除按钮样式

作者: 透支未来 | 来源:发表于2017-04-01 14:32 被阅读333次

    tableview代理方法

    #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
    - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 添加一个删除按钮
        UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");
            
             1. 更新数据
            [_allDataArray removeObjectAtIndex:indexPath.row];
            // 2. 更新UI
            [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
        
        // 删除一个置顶按钮
        UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了置顶");
            
            // 1. 更新数据
            [_allDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
            
            // 2. 更新UI
            NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0inSection:indexPath.section];
            [tableView moveRowAtIndexPath:indexPathtoIndexPath:firstIndexPath];
        }];
        topRowAction.backgroundColor = [UIColor blueColor];
        // 将设置好的按钮放到数组中返回
        return @[deleteRowAction, topRowAction];
    }
    

    自定义cell内

    - (void)layoutSubviews{
        [super layoutSubviews];
        //遍历子视图,找出左滑按钮
        for (UIView *subView in self.subviews)
        {
            if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")])
            {
                for (UIButton *btn in subView.subviews) {
                    if ([btn isKindOfClass:[UIButton class]]) {
                        //更改左滑标签按钮样式
                        if ([btn.titleLabel.text isEqualToString:@"置顶"]) {
                            [btn setTitle:@"" forState:UIControlStateNormal];
                            [btn setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
                        }else if([btn.titleLabel.text isEqualToString:@"删除"]){
                            //更改左滑详情按钮样式
                            [btn setTitle:@"" forState:UIControlStateNormal];
                            [btn setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                        }
                    }
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 自定义cell多个删除按钮样式

          本文链接:https://www.haomeiwen.com/subject/dcpiottx.html