Search code examples
iphoneiosxcodeios4

Custom UITableViewCell editingAccessoryView?


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, here and 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.

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?


Solution

  • 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.