【问题标题】:Is there a way of implementing editActionsForRowAtIndexPath only for a specific UITableViewCell in Swift?有没有办法只为 Swift 中的特定 UITableViewCell 实现 editActionsForRowAtIndexPath ?
【发布时间】:2016-07-18 22:46:51
【问题描述】:

我正在我的应用程序中实现UITableViewController,我想在我的表格视图中添加滑动/滑动单元格的可能性,就像在这个答案中一样:Swipe-able Table View Cell in iOS 9 但不同之处在于它只适用于一个特定的单元格,而不是全部。到目前为止,我有:

class MyTableViewController: UITableViewController {



override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func tableView(tableView: (UITableView!), commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: (NSIndexPath!)) {

}

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

    var deleteAction = UITableViewRowAction(style: .Default, title: "Hello There!") {action in
        print("some action!!")
    }

    return [deleteAction]
}

}

它为所有单元格添加了滑动,我只想将它添加到我列表中的第三个(单元格不是动态生成的)。有没有办法做到这一点?

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    您可以检查作为参数给出的单元格的indexPath,并基于此返回适当的响应。此外,如果您有不同的单元格,您还可以使用func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell? 检查您的单元格的类型。

    索引路径是一个类。您可以使用indexPath.row 从 indexPath 中获取当前行的索引。

    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        if ((indexPath.row % 3) == 0) {
           return true
        } else {
           return false
        }
     }
    

    【讨论】:

    • 但是 indexPath 是一个我可以与数字 3 进行比较的 int 吗?或者在这种情况下我该怎么做?
    • 您可以将indexPath.row 与 3 进行比较
    • 好的,我做到了:if (indexPath.row == 2) { var deleteAction = UITableViewRowAction(... } else { return nil } 但仍然可以滑动每个单元格。在我的第三个单元格中使用该解决方案,我看到字符串Hello There!,当我滑动其他单元格时,我看到字符串Delete。那么有没有办法完全消除滑动其他单元格的可能性? :)
    • 谢谢你,果冻:)
    猜你喜欢
    • 2012-12-15
    • 2011-03-12
    • 2015-12-22
    • 2013-01-06
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多