xcode 自定义 UITableViewCell 编辑AccessoryView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14391797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom UITableViewCell editingAccessoryView?
提问by alan
Here's the dilemma: I want to create a custom editingAccessoryView
that contains two buttons for my stock UITableViewCell
. I'd like to achieve this using a storyboard. So far I've followed the steps outlined here, hereand here. I just can't seem to get it to work. The closest I got was when I created a xib of type UIView
, set the class to that of my UIViewController
that contains the UITableView
and bound it to my IBOutlet
, but on cellForRowAtIndexPath
it's nil
.
这是一个困境:我想editingAccessoryView
为我的股票创建一个包含两个按钮的自定义UITableViewCell
。我想使用故事板来实现这一点。到目前为止,我已按照此处、此处和此处概述的步骤进行操作。我似乎无法让它发挥作用。我得到的最接近的是当我创建一个类型为 xib 的类时UIView
,将类设置为UIViewController
包含 的myUITableView
并将其绑定到 my IBOutlet
,但在cellForRowAtIndexPath
它的nil
.
Truth is, I think I just need to know how to create the view and then map it to the editAccessoryView
; from there I believe I can figure out how to add buttons and map the corresponding IBAction
. Can anyone provide some step-by-step instructions or links to tutorials?
事实是,我想我只需要知道如何创建视图,然后将其映射到editAccessoryView
; 从那里我相信我可以弄清楚如何添加按钮并映射相应的IBAction
. 谁能提供一些分步说明或教程链接?
回答by alan
I solved this on my own with the following code:
我用以下代码自己解决了这个问题:
UIView *editingCategoryAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 35)];
UIButton *addCategoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[addCategoryButton setTitle:@"Add" forState:UIControlStateNormal];
[addCategoryButton setFrame:CGRectMake(0, 0, 50, 35)];
[addCategoryButton addTarget:self action:@selector(addCategoryClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
UIButton *removeCategoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[removeCategoryButton setTitle:@"Remove" forState:UIControlStateNormal];
[removeCategoryButton setFrame:CGRectMake(55, 0, 65, 35)];
[removeCategoryButton addTarget:self action:@selector(removeCategoryClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[editingCategoryAccessoryView addSubview:addCategoryButton];
[editingCategoryAccessoryView addSubview:removeCategoryButton];
cell.editingAccessoryView = editingCategoryAccessoryView;
As you can see I created a new UIView
programmatically and added two buttons via addSubview
and then assigned it to the editingAccessoryView
.
如您所见,我以UIView
编程方式创建了一个新按钮并添加了两个按钮addSubview
,然后将其分配给editingAccessoryView
.
回答by Fomentia
I know this is probably too late to help, but it is much better than the solution you found. iOS gives you a method named tableView: editActionsForRowAtIndexPath indexPath:
. This method basically allows you to add your own UITableViewRowActions, which is much easier (and cleaner) than adding an entire UIView with UIButtons.
我知道这可能为时已晚,但它比您找到的解决方案要好得多。iOS 为您提供了一个名为tableView: editActionsForRowAtIndexPath indexPath:
. 这个方法基本上允许你添加你自己的 UITableViewRowActions,这比用 UIButtons 添加整个 UIView 更容易(也更干净)。
Apple says:
苹果 说:
Use this method when you want to provide custom actions for one of your table rows. When the user swipes horizontally in a row, the table view moves the row content aside to reveal your actions. Tapping one of the action buttons executes the handler block stored with the action object.
If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row.
当您想为表格行之一提供自定义操作时,请使用此方法。当用户在一行中水平滑动时,表格视图将行内容移到一边以显示您的操作。点击其中一个动作按钮会执行与动作对象一起存储的处理程序块。
如果你没有实现这个方法,当用户滑动行时,表格视图会显示标准的附件按钮。
You can look at the Apple Documentationyourself, if you want.
如果需要,您可以自己查看Apple 文档。
Example (Swift)
示例(斯威夫特)
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
let customAction = UITableViewRowAction(style: .Normal, title: "Your Custom Action", handler: { (action: UITableViewRowAction!, indexPath: NSIndexPath!) in
println("Do whatever it is you want to do when they press your custom action button")
})
editAction.backgroundColor = UIColor.greenColor()
let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete", handler: { (action: UITableViewRowAction!, indexPath: NSIndexPath!) in
println("You can even implement a deletion action here")
})
deleteAction.backgroundColor = UIColor.redColor()
return [deleteAction, editAction]
}