自定义编辑按钮到分段控制TableViews

时间:2015-07-02 02:30:13

标签: ios swift uitableview uisegmentedcontrol delete-row

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

我想为每个segmentControl的TableView添加一个刷卡删除功能。但我希望Segment1有1个Button,而Segment2有2个Buttons。

例如:

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

我怎么能这样做,目前我一直在Segment1上获得一个空白区域,点击时会崩溃应用程序。无论如何要隐藏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];
}

1 个答案:

答案 0 :(得分:1)

在0的情况下返回一个UITableViewRowAction,在case 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];
}