首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >切片UITableView细胞-重复细胞

切片UITableView细胞-重复细胞
EN

Stack Overflow用户
提问于 2019-04-18 03:26:41
回答 2查看 68关注 0票数 0
  1. 我试图根据我的Firebase数据库中的一个键来分割我的Tableview数据。
  2. 我能够基于键(itemPreset)正确地分割所有内容。
  3. 我很难将可重用的单元分配到它们的部分。
  4. 单元格不断重复使用每个单元格中相同的文本值。
  5. 每个单元格的行数是正确的,节标题标题是正确的。

这是我的密码-

代码语言:javascript
复制
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
}

SubCategoryCellInfo:

代码语言:javascript
复制
class SubCategoryCellInfo{
var itemPreset: String?

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

解决方案:我基于itemPreset将数组分组,然后使用该部分

代码语言:javascript
复制
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
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-21 17:52:45

解决方案:基于itemPreset将数组分组为区段,然后使用该节。

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

代码语言:javascript
复制
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
}
票数 0
EN

Stack Overflow用户

发布于 2019-04-18 04:03:36

在您的SubCategoryTableViewCell中编写以下代码。

代码语言:javascript
复制
override func prepareForReuse() {
     super.prepareForReuse()
     nameOfLocationText.text = nil
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55738718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档