简体   繁体   English

始终将单元格保持在 uitableview 的顶部

[英]Always keep cell on the top of uitableview

I have an application that is viewbased and I am adding a uitableview as a subview to the main view.我有一个基于视图的应用程序,我将uitableview作为子视图添加到主视图。 The uitableview is full with class Category cells. uitableview充满了 class 类别电池。 Everything works fine, but I want to have “Quick notes” Category always on the top of the uitableview .一切正常,但我希望“快速笔记”类别始终位于uitableview的顶部。 This means when I reloadData() in the Array, “Quick Notes” is always with index 0 and it goes on the bottom of the uitableview .这意味着当我在数组中 reloadData() 时,“快速注释”总是索引为 0,它位于uitableview的底部。 And when I create new cell I need it to go under the “Quick Notes” section.当我创建新单元格时,我需要它到“快速注释”部分下的 go 。 Please help me, what code I need to achieve that functionality and where I need to put it.请帮助我,我需要什么代码来实现该功能以及我需要把它放在哪里。 Thanks!谢谢!

Edit: Thats where I am adding "Quick Notes" to the Realm database.编辑:这就是我向 Realm 数据库添加“快速注释”的地方。

private let categories = try! Realm()

private init() {
    if categories.objects(Category.self).isEmpty {
        createCategoryWith(title: "Quick Notes", color: "#FF0000", icon: "quickNotes")
    }
}

Update the array in the ViewController:更新 ViewController 中的数组:

func didCreateCategory(category: Category) {
    RealmHandler.shared.createCategoryWith(title: category.title, color: category.color, icon: category.icon)
    self.categories = RealmHandler.shared.getAllCategories()
    tableView.reloadData()
}

DataSourceDelegate:数据源委托:

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
}

use viewForHeaderInSection delegate method of tableView.使用 tableView 的viewForHeaderInSection委托方法。

tableView(_ tableView: UITableView, viewForHeaderInSection section: Int)

here you can define your header cell.在这里你可以定义你的 header 单元。 then implement your logic that what do you want to show on this cell.然后实现你想在这个单元格上显示什么的逻辑。 this headerCell will be always on top of your tableView.此 headerCell 将始终位于您的 tableView 之上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM