Search code examples
iosswiftuitableviewuisegmentedcontroldelete-row

Custom Edit Buttons to Segmented Control TableViews


I have a tableViewController with a segmentedControl. Everything works great, the data show up as its suppose to on each tableview and I can switch between each segment control.

I would like to add a Swipe Delete Feature to each segmentControl's TableView. But I want Segment1 to have 1 Button, and the Segment2 to have 2 Buttons.

Ex:

Segment 1
  Button: More
Segment 2
  Button: More
  Button: Delete

How can I do this, at the moment I keep getting a blank space on Segment1 that crashes the app when clicked. Is there anyway to hide that blank space/button from Segment1?

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath     
  indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

    var table:UITableViewCellEditingStyle = UITableViewCellEditingStyle.None

    switch (self.segmentControl.selectedSegmentIndex) {
    case 0:
        table = UITableViewCellEditingStyle.Delete
    case 1:
        table = UITableViewCellEditingStyle.Delete
    default:
        break
    }

    return table

}

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath 
indexPath: NSIndexPath) -> [AnyObject]? {

    var moreRowAction = UITableViewRowAction()
    var deleteRowAction = UITableViewRowAction()

    switch (self.segmentControl.selectedSegmentIndex) {
    case 0:
        moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{action, indexpath in
            println("MORE•ACTION");
        });

    case 1:
        moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{action, indexpath in
            println("MORE•ACTION");
        });
        moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);

        deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
            println("DELETE•ACTION");
        });
    default:
        break
    }

    return [deleteRowAction, moreRowAction];
}

Solution

  • Return one UITableViewRowAction in case 0,return two UITableViewRowAction in case 1, Try this

    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    
        var moreRowAction = UITableViewRowAction()
        var deleteRowAction = UITableViewRowAction()
    
        switch (self.segmentControl.selectedSegmentIndex) {
        case 0:
            moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{action, indexpath in
                println("MORE•ACTION");
            });
            return [moreRowAction];
        case 1:
            moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{action, indexpath in
                println("MORE•ACTION");
        });
            moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);
    
            deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
                println("DELETE•ACTION");
            });
            return [deleteRowAction, moreRowAction];
        default:
            break
        }
    
        return [deleteRowAction, moreRowAction];
    }