【发布时间】: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