如何限制只在某些用户中删除TableView中的行?

时间:2016-05-13 18:32:52

标签: ios swift uitableview row delete-row

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

3 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

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

Reference for editingStyleForRowAtIndexPath

答案 2 :(得分:0)

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

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