【发布时间】:2015-03-07 10:54:38
【问题描述】:
目前我正在使用 NSFetchedResultsController 来处理表格视图。 我想知道有没有办法在滑动操作中添加额外的按钮,比如删除按钮?
我正在考虑对它进行子类化。但是在帮助文档中找不到与我的问题相关的内容。
提前致谢。
【问题讨论】:
标签: ios tableview customization swipe
目前我正在使用 NSFetchedResultsController 来处理表格视图。 我想知道有没有办法在滑动操作中添加额外的按钮,比如删除按钮?
我正在考虑对它进行子类化。但是在帮助文档中找不到与我的问题相关的内容。
提前致谢。
【问题讨论】:
标签: ios tableview customization swipe
您的标题具有误导性。 NSFetchedResultsController 与您的最终目标无关。子类化只会创建不必要的工作,您要查看的是UITableViewRowAction。这很容易处理并且与新的UIAlertController 类似,所以如果你已经涉足UIAlertController,你应该对此感到非常舒服
UITableViewRowAction 的简介(直接来自the docs):
请求代表响应在指定行中滑动而显示的操作。 (必需的) 当您想为您的表格行之一提供自定义操作时,请使用此方法。当用户在一行中水平滑动时,表格视图将行内容移到一边以显示您的操作。点击其中一个操作按钮会执行与操作对象一起存储的处理程序块。 如果不实现此方法,表格视图会在用户滑动行时显示标准的附属按钮。
开始前的几点说明:
无论如何,要实现自定义UITableViewRowAction,您必须首先通过调用以下方法确保您的表格是可编辑的:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//Obviously, if this returns no, the edit option won't even populate
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//Nothing gets called here if you invoke `tableView:editActionsForRowAtIndexPath:` according to Apple docs so just leave this method blank
}
至少,这两个方法需要在继续之前声明。
要真正自定义您的行操作按钮,请遵循以下一般准则:
第一步实现tableView:editActionsForRowAtIndexPath:方法
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
}
如你所见,它是一个NSArray类型的实例方法,所以你必须返回一个数组。
步骤 2 添加动作对象以传入数组。一个例子:
{
UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
// Delete something here
}];
UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
//Do whatever here : just as an example :
[self presentUIAlertControllerActionSheet];
}];
}
您可以更改这些行操作的一些属性,但请参阅文档以了解完整的自定义选项:
要自定义行动作背景颜色,只需像按钮中的大多数其他动作一样执行它:
delete.backgroundColor = [UIColor redColor];
more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1]; //arbitrary color
第 3 步 如前所述,您必须在实例方法中返回一个数组,因此您的完整代码应类似于以下内容:
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
// Delete something here
}];
delete.backgroundColor = [UIColor redColor];
UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
//Just as an example :
[self presentUIAlertControllerActionSheet];
}];
more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1];
return @[delete, more]; //array with all the buttons you want. 1,2,3, etc...
}
进一步参考:LINK TO DOCUMENTATION
针对 SWIFT 语法编辑
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//Do something
})
delete.backgroundColor = UIColor.redColor()
var more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//Do something
})
more.backgroundColor = UIColor.blueColor()
return [delete, more]
}
【讨论】:
@soulshined 接受的答案的所有荣誉。
这是它的 Swift 3 版本:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .default, title: "Delete") { (action:UITableViewRowAction, indexPath:IndexPath) in
print("delete at:\(indexPath)")
}
delete.backgroundColor = .red
let more = UITableViewRowAction(style: .default, title: "More") { (action:UITableViewRowAction, indexPath:IndexPath) in
print("more at:\(indexPath)")
}
more.backgroundColor = .orange
return [delete, more]
}
【讨论】: