【问题标题】:Display different prices in each of the item of the array在数组的每个项目中显示不同的价格
【发布时间】:2018-01-11 06:55:05
【问题描述】:

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

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 的组合,但没有任何帮助,也许我写错了。请帮帮我。

【问题讨论】:

  • 这是你的 allTimeintervalArray ???

标签: ios arrays swift string collections


【解决方案1】:

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

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
}

【讨论】:

  • 谢谢。你的方法对我有用。这是最容易实现的。
【解决方案2】:

您可以使用元组数组(String, String),而不是使用String 的数组,其中包含时间间隔及其各自的价格,即

var firstArrayString: [(String, String)] = [("01:00", 1000), ("02:00", 1000), ("03:00", 1000)]
var secondArrayString: [(String, String)] = [("04:00", 1500), ("05:00", 1500), ("06:00", 1500)]
var thirdArrayString: [(String, String)] = [("07:00", 2000), ("08:00", 2000), ("09:00", 2000), ("10:00", 2000)]

然后在您的collection view 中,您可以像这样使用它:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
    cell.timeLabel.text = allTimeintervalArray[indexPath.item].0
    cell.priceLabel.text = allTimeintervalArray[indexPath.item].1
}

【讨论】:

    【解决方案3】:

    如果您将集合视图分成多个部分(在本例中为三个部分),每个部分对应一个数组,那么您会大有收获。

    var allArrayStrings = [[String]]()
    var allPrices = [Int]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        allArrayStrings = [firstArrayString, secondArrayString, thirdArrayString]
        allPrices = [priceFirst, priceSecond, priceThird]
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return allArrayStrings.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return allArrayStrings[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
    
        cell.timeLabel.text = "\(allArrayStrings[indexPath.section][indexPath.item]) - \(allPrices[indexPath.section])"
    
        return cell
    }
    

    【讨论】:

      【解决方案4】:

      您可以在打印单元格中使用 if 语句来计算数组计数:

      override func viewDidLoad() {
          super.viewDidLoad()
      
      }
      
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      
          return firstArrayString.count + secondArrayString.count + thirdArrayString.count
      
      }
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
      
          if indexPath.item < firstArrayString.count {
              cell.timeLabel.text = "\(firstArrayString[indexPath.item]) - \(priceFirst)"
          }    
          else if indexPath.item < (firstArrayString.count + secondArrayString.count) {
              cell.timeLabel.text = "\(secondArrayString[indexPath.item - firstArrayString.count]) - \(priceSecond)"
          } 
          else {
              cell.timeLabel.text = "\(thirdArrayString[indexPath.item - firstArrayString.count - secondArrayString.count]) - \(priceThird)"
          } 
      
      }
      

      【讨论】:

      • 将每个数组分成自己的部分(如我的回答所示)消除了对所有这些额外数学和if 语句的需要。
      【解决方案5】:

      斯威夫特 4

      我认为这对少量数据有帮助。

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      
          guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as? BookingTimeCell else { return UICollectionViewCell() }
      
          let currentString = allTimeintervalArray[indexPath.item]
      
          if firstArray.contains(currentString) {
              print("Current Array Value:", currentString, "And Price :", firstPrice)
          } else if secondArray.contains(currentString) {
              print("Current Array Value:", currentString, "And Price :", secondPrice)
          } else if thirdArray.contains(currentString) {
              print("Current Array Value:", currentString, "And Price :", thirdPrice)
          } else {
              print("None of the arrays contain", currentString)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-02-20
        • 1970-01-01
        • 2021-06-17
        • 1970-01-01
        • 1970-01-01
        • 2013-01-04
        • 2013-11-19
        • 2013-07-10
        • 1970-01-01
        相关资源
        最近更新 更多