[英]UITableView insert row decoration
我正在嘗試通過在導航欄上提供一個編輯按鈕來啟用我的UITableView
的編輯。
目的是允許用戶插入新項目並刪除現有項目。 單擊編輯按鈕時 iOS 顯示“刪除”裝飾(刪除工作正常),但不顯示“插入”
我已將navigationItem.rightBarButtonItem = editButtonItem
添加到我的UIViewControllers viewDidLoad
並將以下方法添加到我的UITableViewDataSource
委托中:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
AppDelegate.persistenceContext.delete(self.fetchedResultsController.object(at: indexPath))
AppDelegate.saveContext()
} else if editingStyle == .insert {
let current = self.fetchedResultsController.object(at: indexPath)
// do rest of insertion logic
}
}
我錯過了什么?
哦,出於興趣,如果可能的話,新單元格是否會插入到啟動操作的單元格的上方或下方?
作為記錄,我可以向單元格添加自定義滑動操作,但用戶反饋是他們更喜歡編輯按鈕。
任何燈光都將不勝感激,谷歌搜索得到的所有內容都是使用按鈕插入新行,或添加自定義滑動操作。
您需要實現委托方法:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle
為給定的索引路徑返回.delete
、 .insert
或.none
。 通常只有一行(通常是第一行或最后一行)會返回.insert
,而 rest 會返回.delete
。 為無法插入或刪除的任何行返回.none
。
您還需要重寫setEditing(_ editing: Bool, animated: Bool)
方法來添加/刪除額外的行只是為了顯示綠色 + ( .insert
) 圖標,這種情況並不少見。
另一種方法是在導航欄中顯示 + 圖標,而不是在一行中顯示插入圖標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.