【问题标题】: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
}
}
【解决方案3】:
你也可以试试这个,记得给单元格设置默认的editingStyle
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return owner[indexPath.row] == me
}