我有三种不同的字符串数组:
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 和切换的组合,但没有任何帮助,也许我写错了。请帮帮我。
答案 0 :(得分: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
}
答案 1 :(得分:0)
您可以使用包含时间间隔及其各自价格的元组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
}
答案 2 :(得分:0)
如果您将集合视图分成几个部分(本例中为三个),每个数组一个,那么您将会感到很不舒服。
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
}
答案 3 :(得分:0)
您可以在打印单元格中使用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)"
}
}
答案 4 :(得分:0)
Swift 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)
}
}