2016-05-13 54 views
1

我的問題如下。我創建了一個應用程序,允許用戶添加帖子到TableViewController中的行。還可以通過檢查當前用戶是否具有與創建帖子的用戶的ID相同的ID來刪除UITableViewCellEditingStyle的帖子。不幸的是,這種編輯單元格的方式也允許嘗試刪除其他用戶的帖子。我想讓刪除按鈕只在用戶滑過他們的帖子時出現。我附上an image of the button我一直在談論。如何限制在某些用戶中刪除TableView中的行?

回答

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 
    } 
} 
0

只需檢查表格代表editingStyleForRowAtIndexPath中匹配的用戶標識符,如果用戶無效,則返回UITableViewCellEditingStyle.None

Reference for editingStyleForRowAtIndexPath

+0

感謝您的答案,但你可以給我一個如何使用它的一個例子嗎? – Michal

0

你也可以試試這個,記得要設置默認editingStyle細胞

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