分割 UITableView 单元格 - 重复单元格

Posted

技术标签:

【中文标题】分割 UITableView 单元格 - 重复单元格【英文标题】:Sectioning UITableView Cells - Repeating Cells 【发布时间】:2019-04-18 03:26:41 【问题描述】:

    我正在尝试根据我的 Firebase 数据库中的一个键来划分我的 Tableview 数据。

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

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

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

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

这是我的代码 -

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

【问题讨论】:

【参考方案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

【讨论】:

以上是关于分割 UITableView 单元格 - 重复单元格的主要内容,如果未能解决你的问题,请参考以下文章

uitableview中的单元格规范[重复]

UITableView 单元格背景,如“音乐”应用程序 [重复]

两个 UItableview 单元格之间的间隙 3 [重复]

iOS UITableView 单元格被重复

滚动时如何使uitableview单元格不改变?

当 UITableView 单元格中的 UITextView 大小增加时,节标题重复