【问题标题】:How to restrict deleting rows in TableView only to certain users?如何限制删除 TableView 中的行仅对某些用户?
【发布时间】:2016-09-10 01:37:50
【问题描述】:

我的问题如下。我创建了一个应用程序,允许用户向 TableViewController 中的行添加帖子。它还允许他们通过检查当前用户是否具有与创建帖子的用户的 ID 相同的 ID 来仅删除带有 UITableViewCellEditingStyle 的帖子。不幸的是,这种编辑单元格的方式也允许尝试删除其他用户的帖子。我想让删除按钮仅在用户滑动帖子时出现。我附上an image of the button我一直在说。

【问题讨论】:

    标签: ios swift uitableview row delete-row


    【解决方案1】:

    你可以这样做:

    如果你没有 UITableViewCell 的自定义类:

    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        if owner[indexPath.row] == me {
            return .Delete
        }
        else {
            return .None
        }
     }
    

    或者如果您有 UITableViewCell 的自定义类,那么:

    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        if (tableView.cellForRowAtIndexPath(indexPath) as! YourTableViewCell).owner == me {
            return .Delete
        }
        else {
            return .None
        }
    }
    

    【讨论】:

    • 我在这里是什么?
    【解决方案2】:

    只需在 tableview 的委托 editingStyleForRowAtIndexPath 中检查匹配的用户 ID,如果用户无效则返回 UITableViewCellEditingStyle.None

    Reference for editingStyleForRowAtIndexPath

    【讨论】:

    • 谢谢你的回答,但你能给我一个如何使用它的例子吗?
    【解决方案3】:

    你也可以试试这个,记得给单元格设置默认的editingStyle

        func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            return owner[indexPath.row] == me
        }
    

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 1970-01-01
      • 2021-09-04
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 2013-09-12
      相关资源
      最近更新 更多