【问题标题】:Sectioning UITableView Cells - Repeating Cells分割 UITableView 单元格 - 重复单元格
【发布时间】:2019-04-18 03:26:41
【问题描述】:
  1. 我正在尝试根据我的 Firebase 数据库中的一个键来划分我的 Tableview 数据。

  2. 我能够根据密钥 (itemPreset) 正确划分所有内容。

  3. 我无法将可重复使用的单元格分配给它们的部分。

  4. 单元格不断重复,每个单元格中的文本值相同。

  5. 每个单元格的行数正确,节标题标题正确。

这是我的代码 -

var subCategories = [SubCategoryCellInfo]()
var sectionsArray = [String]()

func querySections() -> [String] {
    for selection in subCategories {
        let subCategory = selection.itemPreset
        sectionsArray.append(subCategory ?? "")
    }
    let uniqueSectionsArray = Set(sectionsArray).sorted()
    return uniqueSectionsArray
}

func queryItemPreset(section:Int) -> [Int] {
    var sectionItems = [Int]()
    for selection in subCategories {
        let itemPreset = selection.itemPreset
        if itemPreset == querySections()[section] {
            sectionItems.append(querySections().count)
        }
    }
    return sectionItems
}

func numberOfSections(in tableView: UITableView) -> Int {
    return querySections().count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return querySections()[section]
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering(){
        return filtered.count
    }
    return queryItemPreset(section: section).count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let subCell = tableView.dequeueReusableCell(withIdentifier: "subCell", for: indexPath) as! SubCategoryTableViewCell
    let section = queryItemPreset(section: indexPath.section)

    let task = section[indexPath.row]
    let sub: SubCategoryCellInfo
    if isFiltering(){
        sub = filtered[task]
    }
    else{
        sub = subCategories[task]
    }
    subCell.nameOfLocationText.text = sub.itemPreset
    return subCell
}

子类别单元信息:

class SubCategoryCellInfo{
var itemPreset: String?

init(itemPreset:String?){
    self.itemPreset = itemPreset
}   
}

解决方案: 我根据 itemPreset 将数组分组为多个部分,然后使用该部分

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let subCell = tableView.dequeueReusableCell(withIdentifier: "subCell", for: indexPath) as! SubCategoryTableViewCell
    let groupedDictionary = Dictionary(grouping: subCategories) { (person) -> String in
    return person.itemPreset ?? ""
    }

    var grouped = [[SubCategoryCellInfo]]()

    let keys = groupedDictionary.keys.sorted()

    keys.forEach { (key) in
        grouped.append(groupedDictionary[key]!)
    }

    let task = grouped[indexPath.section]

    let sub: SubCategoryCellInfo
    if isFiltering(){
        sub = filtered[indexPath.row]
    }
    else{
        sub = task[indexPath.row]
    }
    subCell.nameOfLocationText.text = sub.itemPreset
    return subCell
}

【问题讨论】:

    标签: swift firebase uitableview tableview indexpath


    【解决方案1】:

    在您的 SubCategoryTableViewCell 中编写此代码。

    override func prepareForReuse() {
         super.prepareForReuse()
         nameOfLocationText.text = nil
    }
    

    【讨论】:

    • 谢谢,我明天试试,然后告诉你。
    • 不幸的是,这不起作用。细胞仍然在重复自己。如果我做 sub = subCategories[indexPath.row] 它将单元格放入正确的行但不是正确的部分,如果我放入 sub = subCategories[indexPath.section] 它会将单元格放入正确的部分但不是正确的行.
    • 想通了,将解决方案添加到主要问题中。
    【解决方案2】:

    解决方案:根据 itemPreset 将数组分组为多个部分,然后使用该部分。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let subCell = tableView.dequeueReusableCell(withIdentifier: "subCell", for: indexPath) as! SubCategoryTableViewCell
    let groupedDictionary = Dictionary(grouping: subCategories) { (person) -> String in
    return person.itemPreset ?? ""
    }
    
    var grouped = [[SubCategoryCellInfo]]()
    
    let keys = groupedDictionary.keys.sorted()
    
    keys.forEach { (key) in
        grouped.append(groupedDictionary[key]!)
    }
    
    let task = grouped[indexPath.section]
    
    let sub: SubCategoryCellInfo
    if isFiltering(){
        sub = filtered[indexPath.row]
    }
    else{
        sub = task[indexPath.row]
    }
    subCell.nameOfLocationText.text = sub.itemPreset
    return subCell
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-04
      • 2012-01-04
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多