【问题标题】:Cell Only Shows Last String in Array单元格仅显示数组中的最后一个字符串
【发布时间】:2017-09-06 21:15:59
【问题描述】:

我有一个对象数组,我正在尝试将数组的正确索引分配给某个单元格,如下所示。

我的数组:

let nflTeamData: [String] = ["New England Patriots", "Miami Dolphins", "New York Jets", "Buffalo Bills", "Baltimore Ravens", "Cincinnati Bengals", "Pittsburgh Steelers", "Cleveland Browns", "Tampa Bay Buccaneers", "New Orleans Saints", "Carolina Panthers", "Atlanta Falcons", "San Francisco 49ers", "Seattle Seahawks", "Arizona Cardinals", "Los Angeles Rams"]

下面是将虚拟数据添加到 CoreData 的位置。我在下面打印出teamName,但不幸的是,我只获取了我的数组(洛杉矶公羊队)中的最后一项来填充 CollectionView 中的每一行。我知道我需要以某种方式合并数组的索引路径,但不确定在 collectionview 委托方法之外访问它的正确方法。

//MARK: CoreData
func addDummyData() {
    let moc = DataManager.sharedInstance.persistentContainer.viewContext

    for div in nflDivision {
        let division = NSEntityDescription.insertNewObject(forEntityName: "Division", into: moc) as! Division
        print(div)
        division.name = div
        addTeams(division: division)
    }

    DataManager.sharedInstance.saveContext()
}

func addTeams(division: Division) {
    let moc = DataManager.sharedInstance.persistentContainer.viewContext

    var i = 1
    while i < 5 {
        let team = NSEntityDescription.insertNewObject(forEntityName: "Team", into: moc) as! Team

        for (index, teamName) in nflTeamData.enumerated() {
            print(index, teamName)
            team.name = "\(teamName) - \(i)"
        }

        team.id = Int64(i)
        i += 1
        division.addToTeams(team)
    }
}

//MARK: CollectionView

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! StandingsCell
    let division = divisions[indexPath.section]
    let team = division.teams?[indexPath.row] as! Team
    cell.teamLabel.text = team.name
    cell.teamLogo.image = #imageLiteral(resourceName: "nfl58")
    cell.winTextField.delegate = self
    cell.lossTextField.delegate = self
    cell.winTextField.tag = 0 //Increment accordingly
    return cell
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    return divisions.count
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let division = divisions[section]
    return division.teams?.count ?? 0
}

电流输出:

预期输出:

只有一个团队从上面提到的nflTeamData 数组中填充一行。

编辑:我现在尝试根据下面字典中的值编号实施 numberOfRowsInSection 作为可能的解决方案,但仍然出现错误。

let nflDictionary: [String: String] = ["New England Patriots" : "1",
                                       "Miami Dolphins" : "2",
                                       "New York Jets": "3",
                                       "Buffalo Bills": "4",
                                       "Baltimore Ravens": "1",
                                       "Cincinnati Bengals": "2",
                                       "Pittsburgh Steelers": "3",
                                       "Cleveland Browns": "4",
                                       "Houston Texans": "1",
                                       "Jacksonville Jaguars" : "2",
                                       "Indianapolis Colts": "3",
                                       "Tennessee Titans": "4",
                                       "Kansas City Cheifs" : "1",
                                       "Denver Broncos": "2",
                                       "Oakland Raiders": "3",
                                       "Los Angeles Chargers": "4",
                                       "New York Giants": "1",
                                       "Philadelphia Eagles": "2",
                                       "Washington Redskins": "3",
                                       "Dallas Cowboys": "4",
                                       "Minnesota Vikings": "1",
                                       "Green Bay Packers": "2",
                                       "Detriot Lions": "3",
                                       "Chicago Bears": "4",
                                       "Tampa Bay Buccaneers": "1",
                                       "New Orleans Saints": "2",
                                       "Carolina Panthers": "3",
                                       "Atlanta Falcons": "4",
                                       "San Francisco 49ers": "1",
                                       "Seattle Seahawks": "2",
                                       "Arizona Cardinals": "3",
                                       "Los Angeles Rams": "4"
]

【问题讨论】:

  • 您的addTeams 代码毫无意义。为什么while 循环中有一个for 循环?请注意for 循环在while 循环的每次迭代中所做的工作。
  • while 循环用于跟踪属于每个分区的团队数量(每个分区总共 4 个团队,总共 8 个分区)。 for 循环试图访问 nflTeamData 数组中的每个索引。我想我应该分配 team.name = "\(nflTeamData[someIndex])" 但我不知道如何在不使用 for 循环的情况下访问该数组的索引。有什么建议@rmaddy?

标签: swift core-data uicollectionview


【解决方案1】:

看来您需要修改此方法。删除 while 语句并尝试,因为它总是更新相同的值:-

func addTeams(division: Division) {
    let moc = DataManager.sharedInstance.persistentContainer.viewContext
    for (index, teamName) in nflTeamData.enumerated() {
        let team = NSEntityDescription.insertNewObject(forEntityName: "Team", into: moc) as! Team
            print(index, teamName)
            team.name = "\(teamName) - \(index)"
            team.id = Int64(index)
            division.addToTeams(team)
        }
}

【讨论】:

  • 这很接近,但它将所有内容放在我的 collectionView 的一个部分下。我想在每个部分中仅填充 nflTeamData 数组中的 4 个项目。这会将所有 16 个项目放在同一部分中。这就是为什么我需要 while 循环在从 1 开始并且小于 5 时为真。谢谢@Hussain Shabbir
  • 在这种情况下,您需要使用字典,您可以在其中将键定义为部分并迭代数据并相应地显示在您的部分上。有意义吗?
  • 合乎逻辑,但不确定如何实施。也许您可以提供一些指导?
  • 创建您的字典数组(键,值),其中键将是您的 id,然后在您的 numberOfSections 方法中根据新字典中的 id 显示计数。现在迭代您的字典并获取值数组并显示在您的行中。
  • 我尝试过添加字典,但没有成功。如果您可以通过示例提供额外的指导,将不胜感激!我实际上在字典中创建了键值对(id,teamName)。我的理解是,如果 id 等于 1,它应该进入该部分的第一个位置(每个部分只有 4 个团队)。我尝试在 numberofrowsinsection 中实现此功能,但没有成功
【解决方案2】:

请检查:

func addDummyData() {
    let moc = DataManager.sharedInstance.persistentContainer.viewContext

    var divisionLevel = 1
    for div in nflDivision {
        let division = NSEntityDescription.insertNewObject(forEntityName: "Division", into: moc) as! Division
        print(div)
        division.name = div
        addTeams(division: division, divisionLevel: divisionLevel)
        divisionLevel = divisionLevel + 4
    }

    DataManager.sharedInstance.saveContext()
}

func addTeams(division: Division, divisionLevel: Int) {
    let moc = DataManager.sharedInstance.persistentContainer.viewContext
    var i = 1
    for var index in divisionLevel..<divisionLevel+4 {
        if nflTeamData.count >= index {
            let team = NSEntityDescription.insertNewObject(forEntityName: "Team", into: moc) as! Team
            team.name = "\(nflTeamData[index-1]) - \(i)"
            print("\(team.name)")
            team.id = Int64(i)
            division.addToTeams(team)
            i = i + 1
        }
    }
}

输出:

NFC North
Optional("New England Patriots - 1")
Optional("Miami Dolphins - 2")
Optional("New York Jets - 3")
Optional("Buffalo Bills - 4")
NFC East
Optional("Baltimore Ravens - 1")
Optional("Cincinnati Bengals - 2")
Optional("Pittsburgh Steelers - 3")
Optional("Cleveland Browns - 4")
...

【讨论】:

  • 这不起作用。我尝试在我的项目和操场上搞乱,它重复每个团队的 id(即 ["id": "1", "name": "New England Patriots"], , ["id": "2", “名称”:“新英格兰爱国者队”],[“id”:“3”,“名称”:“新英格兰爱国者队”],[“id”:“4”,“名称”:“新英格兰爱国者队”] )
  • 您能否提供您想要获得的示例输出?
  • 在AFC East的标题下(如上图所示),应该列出我阵列中的前四支球队(第一红格的新英格兰爱国者队,第二红格的迈阿密海豚队,纽约喷气机队在第 3 个红色单元格中,布法罗比尔队在第 4 个红色单元格中。这与我的 nflTeamData 数组中的 0 -3 相同。但我目前没有得到那个。我要么从nflTeamData 或标记为同一团队的 4 个项目。
  • 在 AFC North 下它将继续向下移动相同的nflTeamData 阵列并填充(Baltimore Ravens 在 AFC North 部分标题下的第一个红细胞中,辛辛那提孟加拉虎在匹兹堡的第二个红细胞中钢人队在红细胞第 3,克利夫兰布朗队在第 4)
  • 你想要这样的输出:[["id": 1, "name": "New England Patriots - 1"], ["id": 2, "name": "Miami Dolphins - 2"], ["id": 3, "name": "New York Jets - 3"], ["id": 4, "name": "Buffalo Bills - 4"], ["id": 1, "name": "Baltimore Ravens - 1"], ["id": 2, "name": "Cincinnati Bengals - 2"], ["id": 3, "name": "Pittsburgh Steelers - 3"], ["id": 4, "name": "Cleveland Browns - 4"], ....]
猜你喜欢
  • 1970-01-01
  • 2014-11-01
  • 2012-01-27
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
相关资源
最近更新 更多