0
  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
}
4

2 回答 2

0

解决方案:根据 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
}
于 2019-04-21T17:52:45.773 回答
0

在您的SubCategoryTableViewCell内编写此代码。

override func prepareForReuse() {
     super.prepareForReuse()
     nameOfLocationText.text = nil
}
于 2019-04-18T04:03:36.150 回答