我有三个不同的字符串数组:
var firstArrayString: [String] = ["01:00", "02:00", "03:00"]
var secondArrayString: [String] = ["04:00", "05:00", "06:00"]
var thirdArrayString: [String] = ["07:00", "08:00", "09:00", "10:00"]
我有每个字符串数组的价格:
var priceFirst: Int = 1000
var priceSecond: Int = 1500
var priceThird: Int = 2000
我使用 collectionCell 来显示我所有的数组字符串。
override func viewDidLoad() {
super.viewDidLoad()
var allArrayStrings: [String] = firstArrayString + secondArrayString + thirdArrayString
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return allTimeintervalArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
cell.timeLabel.text = allTimeintervalArray[indexPath.item]
// how can I display different prices in each of the rows of the array?
}
我想在我的 collectionCell 中大致显示:
01:00 - 1000
02:00 - 1000
03:00 - 1000
04:00 - 1500
05:00 - 1500
06:00 - 1500
07:00 - 2000
08:00 - 2000
09:00 - 2000
10:00 - 2000
我如何展示它?
我尝试了很多if和switch的组合,但没有任何帮助,也许我写错了。请帮助我。
最佳答案
另一种方法是创建元组数组(价格和字符串值):
let allArrayStringsAndPrices: [(price: Int, value: String)] =
firstArrayString.map({ (price: priceFirst, value: $0) })
+ secondArrayString.map({ (price: priceSecond, value: $0) })
+ thirdArrayString.map({ (price: priceThird, value: $0) })
并使用它:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return allArrayStringsAndPrices.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
cell.timeLabel.text = "\(allArrayStringsAndPrices[indexPath.row].value) - \(allArrayStringsAndPrices[indexPath.row].price)"
return cell
}
关于ios - 在数组的每个项目中显示不同的价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48201340/