自定义 UITableView Cell 中的按钮

Posted

技术标签:

【中文标题】自定义 UITableView Cell 中的按钮【英文标题】:Customize the buttons in UITableView Cell 【发布时间】:2013-12-24 07:22:33 【问题描述】:

我有“滑动删除”TableViewCell 的功能。我想再添加一个删除按钮。请看图

我正在使用这个 UITableView 委托函数来删除 tableview 单元格。

- (void)tableView:(UITableView *)tableView commitEditingStyle:
  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

这可能吗?如何访问 Extra 按钮?

谢谢。

【问题讨论】:

您可以使用按钮制作自定义单元格。在 touchupinside 传递索引路径并从数据源中删除特定数据并重新加载表 我认为不可能再添加一个按钮。尝试进行自定义滑动,以便您可以根据需要添加更多按钮。 你可能想看看这个teehanlax.com/blog/reproducing-the-ios-7-mail-apps-interface 请通过下面的链接,它与您的要求相同。 ios-blog.co.uk/tutorials/… @keyurbhalodiya 谢谢 【参考方案1】:

参考此链接并下载示例应用程序:https://github.com/boctor/idev-recipes/tree/master/SideSwipeTableView

并使用多个按钮自定义您的 uitableviewcell。

 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) + kCatchWidth, CGRectGetHeight(self.bounds));
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;

[self.contentView addSubview:scrollView];
self.scrollView = scrollView;

UIView *scrollViewButtonView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - kCatchWidth, 0, kCatchWidth, CGRectGetHeight(self.bounds))];
self.scrollViewButtonView = scrollViewButtonView;
[self.scrollView addSubview:scrollViewButtonView];

// Set up our two buttons
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.backgroundColor = [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0f];
moreButton.frame = CGRectMake(0, 0, kCatchWidth / 3.0f, CGRectGetHeight(self.bounds));
[moreButton setTitle:@"More" forState:UIControlStateNormal];
[moreButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[moreButton addTarget:self action:@selector(userPressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];

[self.scrollViewButtonView addSubview:moreButton];

UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
shareButton.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f];
shareButton.frame = CGRectMake(kCatchWidth / 3.0f, 0, kCatchWidth / 3.0f, CGRectGetHeight(self.bounds));
[shareButton setTitle:@"Share" forState:UIControlStateNormal];
[shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[shareButton addTarget:self action:@selector(userPressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollViewButtonView addSubview:shareButton];

UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.backgroundColor = [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0f];
deleteButton.frame = CGRectMake(kCatchWidth / 3.0f+kCatchWidth / 3.0f, 0, kCatchWidth / 3.0f, CGRectGetHeight(self.bounds));
[deleteButton setTitle:@"Delete" forState:UIControlStateNormal];
[deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[deleteButton addTarget:self action:@selector(userPressedDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollViewButtonView addSubview:deleteButton];

UIView *scrollViewContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
scrollViewContentView.backgroundColor = [UIColor whiteColor];
[self.scrollView addSubview:scrollViewContentView];
self.scrollViewContentView = scrollViewContentView;

UILabel *scrollViewLabel = [[UILabel alloc] initWithFrame:CGRectInset(self.scrollViewContentView.bounds, 10, 0)];
self.scrollViewLabel = scrollViewLabel;
[self.scrollViewContentView addSubview:scrollViewLabel];

我已经用我的应用程序实现了这段代码,得到了这样的结果。您可以和单元格滑动中的按钮数量。

这里是实现的屏幕截图

滑动单元格3个按钮后出现“更多”、“分享”、“删除”。

【讨论】:

没关系,伙计,我已经可以做到这一点,无论如何感谢您的帮助

以上是关于自定义 UITableView Cell 中的按钮的主要内容,如果未能解决你的问题,请参考以下文章

iOS UITableView

iOS Table View Cell 自定义左滑按钮及事件(系统自带方法)

iOS UITableView-自定义Cell

iOS UITableView-自定义Cell

iOS-UITableView-处理cell上按钮事件(弹出警示框,页面跳转等)

UITableView(自定义cell)试水心得