【发布时间】:2018-08-05 17:19:17
【问题描述】:
我有一个 UICollectionView,里面有多个单元格。每个单元格都有一个标签,其中包含以下数据。
我想做的是对于不是“A”或“B”的标签文本,我希望该值是基于单元格顺序的 Int 值。
示例
此刻的Cells Label文本:
A、A、3、B、5、A、B
我想要什么:
A、A、1、B、2、A、B
非常感谢任何帮助,非常感谢!
代码:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! DataCell
let integerArray = [Int](1...10)
let sortedCellData = cellData.sorted { $0.key < $1.key}
if String(Array(sortedCellData)[indexPath.row].value) == "A" {
cell.valueLabel.text = "A"
} else if String(Array(sortedCellData)[indexPath.row].value) == "B" {
cell.valueLabel.text = "B"
} else {
cell.valueLabel.text = String(integerArray[indexPath.row])
}
return cell
}
}
【问题讨论】:
-
我不明白
A, A, 3, B, 5, A, B和预期结果A, A, 1, B, 2, A, B之间的排序。你能说清楚吗?它是否保留字母,并且 Int 应该从 1 开始替换为“增加”?此外,在加载UICollectionView之前对其进行排序,而不是在cellForItemAt中进行排序,因为您每次都在做一次又一次,而它可以做一次。 -
嗨,Larme,是的,这就是我的意思。所以目前 cell(3) 是“3”,而我希望它是“1”,而 cell(5) 是“5”,而我希望它是“2”。如果这更有意义?感谢您的帮助,如果我不够清楚,请见谅!!
标签: ios arrays swift if-statement uicollectionview