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

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

推荐阅读更多精彩内容

  • 内存管理是程序在运行时分配内存、使用内存,并在程序完成时释放内存的过程。在Objective-C中,也被看作是在众...
    蹲瓜阅读 8,443评论 1 8
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,734评论 25 709
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,950评论 9 468
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,567评论 19 139
  • 记录一下字符编码的有关事项,这篇文章先说说字符编码的一些历史和原理。 1.ASCII编码与ANSI标准 1字节(1...
    pmonkey阅读 6,435评论 3 31