如何向UITableViewCell添加更多按钮滚动时?

当我们滚动(向左滑动) UITableViewCell ,它会显示一个删除按钮,但我想在其上添加其他按钮,我该怎么办?

我想要的风格就像iOS 7中的系统邮件应用程序,UITableviewCell中有两个按钮,一个是删除按钮,另一个是更多按钮。

请提出任何想法

谢谢

您可以使用此示例方法创建更多按钮

https://github.com/scheinem/MSCMoreOptionTableViewCell

在此处输入图像描述

此链接示例更有用,您可以创建自定义更多按钮。

https://github.com/CEWendel/SWTableViewCell

在此处输入图像描述

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { NSMutableArray *leftUtilityButtons = [NSMutableArray new]; NSMutableArray *rightUtilityButtons = [NSMutableArray new]; [leftUtilityButtons addUtilityButtonWithColor: [UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0] icon:[UIImage imageNamed:@"check.png"]]; [leftUtilityButtons addUtilityButtonWithColor: [UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0] icon:[UIImage imageNamed:@"clock.png"]]; [leftUtilityButtons addUtilityButtonWithColor: [UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0] icon:[UIImage imageNamed:@"cross.png"]]; [leftUtilityButtons addUtilityButtonWithColor: [UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0] icon:[UIImage imageNamed:@"list.png"]]; [rightUtilityButtons addUtilityButtonWithColor: [UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] title:@"More"]; [rightUtilityButtons addUtilityButtonWithColor: [UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"Delete"]; cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier containingTableView:_tableView // For row height and selection leftUtilityButtons:leftUtilityButtons rightUtilityButtons:rightUtilityButtons]; cell.delegate = self; } NSDate *dateObject = _testArray[indexPath.row]; cell.textLabel.text = [dateObject description]; cell.detailTextLabel.text = @"Some detail text"; return cell; } 

如此处所述,您可以使用其他UITableView数据源/委托方法为Mail.app实现与单元格相同的布局

 - (NSString *)tableView:(UITableView *)tableView titleForSwipeAccessoryButtonForRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView swipeAccessoryButtonPushedForRowAtIndexPath:(NSIndexPath *)indexPath; 

可能性,请按照下列步骤操作:

1)委托函数canEditRowAtIndexPath应该返回NO ;

2)使用cellForRowAtIndexPath在UITableView中加载自定义单元格。

3)在自定义单元格中,显示两个或更多按钮并使用以下方法处理可见性:

 [btn setHidden:NO]; 

希望它可以给你一个想法,如何开始。

这真的很容易,不要重新发明轮子使用这个控件(自定义UITableViewCell)它是开源的,并且工作得非常好。

如果您不想只是放入并使用它,那么至少您可以准确地阅读它们是如何完成的! https://www.cocoacontrols.com/controls/swtableviewcell

Apple已经为此添加了自己的支持

 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { }]; editAction.backgroundColor = [UIColor blueColor]; UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete" handler^(UITableViewRowAction *action, NSIndexPath *indexPath) { }]; return @[deleteAction, editAction]; }