ios - 在数组的每个项目中显示不同的价格

标签 ios arrays swift string collections

我有三个不同的字符串数组:

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

我如何展示它?

我尝试了很多ifswitch的组合,但没有任何帮助,也许我写错了。请帮助我。

最佳答案

另一种方法是创建元组数组(价格和字符串值):

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/

相关文章:

iOS Core Data 对多关系插入/获取

objective-c - 通过滑动 UIView 滑出导航以覆盖另一个 UIView

arrays - 数组中每个 "_id"的别名

swift - 如何在子类中覆盖 tableView 样式

ios - 离线启动应用程序时出现 Realm NSInternalInconsistencyException

objective-c - 如何在 iOS 中制作 "Identify browser as"

ios - 如何在不使用 "UIDocumentPickerViewController"的情况下从 Files 应用程序枚举文件?

c - 如何在 C 中打印 double 数组?

arrays - 动态填充空单元格

ios - 为什么 WKWebView 不显示视频? - swift 3