美文网首页
UITableViewCell左滑显示多个图片按钮并改变大小

UITableViewCell左滑显示多个图片按钮并改变大小

作者: Felix灬泡泡 | 来源:发表于2017-04-13 18:10 被阅读0次

    第一步:在TableViewController.m文件中添加:

    ///返回装载侧边按钮的数组
    - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
        // 添加一个电话按钮
        UITableViewRowAction *telephoneRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"电话" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了电话按钮");
        }]; telephoneRowAction.backgroundColor = [UIColor blueColor];
        
        // 添加一个短信按钮
        UITableViewRowAction *messageRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"短信" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了短信按钮");
        }]; messageRowAction.backgroundColor = [UIColor yellowColor];
        
        // 添加一个编辑按钮
        UITableViewRowAction *editRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了编辑按钮");
        }]; editRowAction.backgroundColor = [UIColor redColor];
        
        // 将设置好的按钮放到数组中返回(先添加的在右侧)
        return @[editRowAction, messageRowAction, telephoneRowAction];
    }
    

    第二步:在TableViewCell.m文件中添加

    ///cell状态已经转换时调用的函数
    - (void)didTransitionToState:(UITableViewCellStateMask)state {
        [super didTransitionToState:state];
        
        NSDictionary *imgDic = @{@"电话": @"telephone", @"短信": @"message", @"编辑": @"edit"};
        if (state == UITableViewCellStateShowingDeleteConfirmationMask) {
            for (UIView *subview in self.subviews) {
                if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
                    subview.backgroundColor = [UIColor clearColor];
                    for (UIView *actionBtn in subview.subviews) {
                        if ([NSStringFromClass([actionBtn class]) isEqualToString:@"_UITableViewCellActionButton"]) {
                            /* 这里可以改变Action Button的大小 */
                            CGRect frame = actionBtn.frame;
                            frame.size.height -= 10;
                            actionBtn.frame = f;
    
                            /* 这里可以改变Action Button的信息 */
                            UIButton *btn = (UIButton *)actionBtn;
                            NSString *imgStr = btn.titleLabel.text;
                            [btn setTitle:@"" forState:UIControlStateNormal];
                            [btn setImage:[UIImage imageNamed:imgDic[imgStr]] forState:UIControlStateNormal];
                        }
                    }
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:UITableViewCell左滑显示多个图片按钮并改变大小

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