【问题标题】:Custom Edit Buttons to Segmented Control TableViews自定义编辑按钮到分段控件表视图
【发布时间】:2015-07-02 02:30:13
【问题描述】:

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

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

例如:

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

【问题讨论】:

    标签: ios swift uitableview uisegmentedcontrol delete-row


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2011-05-22
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多