我有一個基于視圖的應用程式,我正在將一個uitableview作為子視圖添加到主視圖。uitableview充滿了類類別單元格。一切正常,但我希望“快速筆記”類別始終位于uitableview的頂部。這意味著當我在陣列中 reloadData() 時,“快速筆記”的索引始終為 0,并且位于uitableview的底部。當我創建新單元格時,我需要它進入“快速注釋”部分。請幫助我,我需要什么代碼來實作該功能以及我需要把它放在哪里。謝謝!
編輯:這就是我將“快速筆記”添加到領域資料庫的地方。
private let categories = try! Realm()
private init() {
if categories.objects(Category.self).isEmpty {
createCategoryWith(title: "Quick Notes", color: "#FF0000", icon: "quickNotes")
}
}
更新 ViewController 中的陣列:
func didCreateCategory(category: Category) {
RealmHandler.shared.createCategoryWith(title: category.title, color: category.color, icon: category.icon)
self.categories = RealmHandler.shared.getAllCategories()
tableView.reloadData()
}
資料源委托:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath)
cell.textLabel?.text = categories[categories.count - (1 indexPath.row)].getTitle()
cell.contentView.backgroundColor = hexStringToUIColor(hex: categories[categories.count-(1 indexPath.row)].getColor())
return cell
}
uj5u.com熱心網友回復:
使用viewForHeaderInSectiontableView 的委托方法。
tableView(_ tableView: UITableView, viewForHeaderInSection section: Int)
在這里你可以定義你的標題單元格。然后實作你想在這個單元格上顯示什么的邏輯。此 headerCell 將始終位于您的 tableView 之上。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/427936.html
下一篇:從概念中獲取資料庫內容
