![](/img/trans.png)
[英]Access section header button in tableview when user clicks on a row of that section?
[英]TableView section header moves when a row is edited
我正在使用tableView構建一個iOS應用,它使用Realm數據庫作為數據模型。 當我嘗試通過向左拖動單元格來刪除行時,節標題跟隨向左移動。 當按下刪除鍵時,單元格將被刪除,並且節標題將移回到正確的位置。
為什么節標題隨單元格一起移動的任何線索?
標題單元在情節提要中定義為動態原型單元,行單元定義在單獨的xib中並在表格視圖中注冊。 剖面單元的情節提要中未選中“編輯時縮進”選項。
“每周報告”位於部分標題中。
這是我實現的啟用編輯的代碼:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
try! realm.write {
let reportToDelete = reportList[indexPath.row]
realm.delete(reportToDelete)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
}
我在設備和兩個不同的模擬器上都嘗試過,並在build文件夾中進行了清理。
編輯
該標題是從情節提要中加載的,該故事板上具有可重復使用的標識符:“ WeeklyReportHeader”,而UIView由tableView的委托返回。
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableCell(withIdentifier: "WeeklyReportHeader")! as UIView
}
我認為這與使用單元格作為標頭有關,可能與單元重用有關。
要解決此問題,而不是使用單元格本身(並將其強制轉換為標題的UIView),請使用它的contentView屬性,該屬性始終是UIView。
例如
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableCell(withIdentifier: "WeeklyReportHeader")!.contentView
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.