自定义编辑按钮以分段控制 TableView

Custom Edit Buttons to Segmented Control TableViews

我有一个带有 segmentedControl 的 tableViewController。一切都很好,数据显示在每个表视图上,我可以在每个段控件之间切换。

我想为每个 segmentControl 的 TableView 添加滑动删除功能。但我希望 Segment1 有 1 个按钮,Segment2 有 2 个按钮。

例如:

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

我该怎么做,目前我在 Segment1 上一直出现空白 space,单击时会导致应用程序崩溃。无论如何要从 Segment1 中隐藏空白 space/button?

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];
}

Return 案例 0 中的一个 UITableViewRowAction,return 案例 1 中的两个 UITableViewRowAction,试试这个

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];
}