遍历已分组的数组的问题

时间:2018-10-30 08:42:01

标签: ios uitableview uicollectionview swift4 xcode10

我有一个tableViewCell,其中包含一个collectionView,它将显示一些单元格。我已将数据链接到Firebase,将在此tableView中显示。

我目前遇到的一个问题是,使用新的Dictionary分组功能(我在TableViewCell中所做的)将某个结构的数组进行了分组。

完成此操作后,我尝试在cellforItemAtIndexPath的collectionView单元中对此进行迭代。

我注意到的是,当我运行代码时,我的单元格显示了迭代的后半部分,因此显示了它的最后一部分。

请在下面的tableView代码中找到

struct WebsiteStruct{
var title: String
var description: String
var banner: String
var link: String
}

var siteInfos: [WebsiteStruct] = []
var siteSections: [String] = []

class Websites: UIViewController, UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()

    let websitesRef = Database.database().reference().child("Schools").child(Variables.schoolName).child("Websites")

    websitesRef.observe(.value, with: { snapshot in
        for site in snapshot.children {
            if let siteSnapshot = site as? DataSnapshot{
                let siteInfo = siteSnapshot.value as? NSDictionary
                siteInfos.append(WebsiteStruct(title: siteInfo?.value(forKey: "title") as! String, description: siteInfo?.value(forKey: "description") as! String, banner: siteInfo?.value(forKey: "banner") as! String, link: siteInfo?.value(forKey: "link") as! String))
            }
        }
        self.tableView.reloadData()
    })
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    for website in siteInfos{
        let section = website.description
        if !siteSections.contains(section){
            siteSections.append(section)
        }
    }
    return siteSections.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "websiteSectionCell") as! WebsiteSectionCell
    cell.displayContent(sectionTitle: siteSections[indexPath.row], indexPath: indexPath.row)
    return cell
}
}

这是我的collectionViewCell的代码:

class WebsiteSectionCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource{

@IBOutlet weak var websiteCornerView: UIView!
@IBOutlet weak var Title: UILabel!
@IBOutlet weak var websiteCollectionView: UICollectionView!

var currentIndexPath: Int = 0
let groupedSites = Dictionary(grouping: siteInfos, by: { $0.description })

func displayContent(sectionTitle: String, indexPath: Int){
    websiteCornerView.layer.cornerRadius = Variables.cornerRadius
    websiteCornerView.layer.masksToBounds = true

    Title.text = sectionTitle
    currentIndexPath = indexPath
}

override func awakeFromNib() {
    super.awakeFromNib()
    websiteCollectionView.dataSource = self as UICollectionViewDataSource
    websiteCollectionView.delegate = self as UICollectionViewDelegate
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    //Set selected
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    for site in groupedSites[siteSections[currentIndexPath]]!{
        print(site.title)
    }
    print(groupedSites[siteSections[currentIndexPath]]!)
    return groupedSites[siteSections[currentIndexPath]]!.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! WebsiteCell
    for site in groupedSites[siteSections[currentIndexPath]]!{
        cell.displayContent(title: site.title, banner: Variables.schoolBanner!)
        print(indexPath)
    }
    return cell
}
}

下面是它的屏幕截图。

Normally, the "School" cells should be Test Site 1 and Test site 2, not only Test site 2. Same goes for the utility cells.

我不知道它是否有某种联系,但是collectionView cellForItemAt indexPath函数似乎只运行了2次代码,却运行了4次代码。

一些重要信息:我的所有数据均已正确分组,当我在控制台中打印它们时,将显示正确的数组:

[EEB3App.WebsiteStruct(title: "Site 1", description: "School", banner: "/", link: "/"), EEB3App.WebsiteStruct(title: "Site 2", description: "School", banner: "/", link: "/")]

[EEB3App.WebsiteStruct(title: "Site 3", description: "Utility", banner: "/", link: "/"), EEB3App.WebsiteStruct(title: "Site 4", description: "Utility", banner: "/", link: "/")]

1 个答案:

答案 0 :(得分:0)

首先,您的代码很难阅读。我建议至少将结构重构到另一个文件中。

另一件事是您需要检查此特定代码行。它不是有序数组,可能会向您返回相同的值。

let groupedSites = Dictionary(grouping: siteInfos, by: { $0.description })

应该在WebsitesViewController中声明并使用它,而不是在单元格中执行逻辑。您可以将siteSections的数组和siteInfos的数组替换为此。