tableview在`didSelect`事件上显示更多选项

时间:2018-10-17 10:16:38

标签: ios swift uitableview swipe

如何在didselectRow:上显示uitableview的滑动选项

要显示如下滑动选项。enter image description here

示例显示在图中,这应该在didselect行中实现。如何实现这一点对我有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您应该确保:

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // the cells you would like the actions to appear needs to be editable
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // you need to implement this method too or you can't swipe to display the actions
}

第二步,您将在此委托方法中实现:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
        println("more button tapped")
    }
    more.backgroundColor = UIColor.lightGrayColor()

    let favorite = UITableViewRowAction(style: .Normal, title: "Favorite") { action, index in
        println("favorite button tapped")
    }
    favorite.backgroundColor = UIColor.orangeColor()

    let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
        println("share button tapped")
    }
    share.backgroundColor = UIColor.blueColor()

    return [share, favorite, more]
}

最终视图如下: enter image description here 希望这对您有帮助。