Here\'s the dilemma: I want to create a custom editingAccessoryView
that contains two buttons for my stock UITableViewCell
. I\'d like to achieve
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
.
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.
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 Documentation yourself, if you want.
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]
}