gpt4 book ai didi

swift - 如何在swift 4中的 Collection View 中实现字符串数组的分页

转载 作者:行者123 更新时间:2023-11-30 11:23:39 25 4
gpt4 key购买 nike

import UIKit

class ProductListViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

@IBOutlet weak var productListCell: UICollectionView!

var manArray = ["Men","m1","m2","m3","m4","m5","m6","m7"]
var mPrice = ["789","1259","959","1625","1259","936","980","1500"]

override func viewDidLoad() {

super.viewDidLoad()

productListCell.delegate = self
productListCell.dataSource = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return manArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ListCollectionViewCell", for: indexPath) as! ListCollectionViewCell
cell.productImg.image = UIImage(named: manArray[indexPath.row])
cell.productlbl.text = "\(manArray[indexPath.row])"
cell.productprice.text = "\(mPrice[indexPath.row])"

return cell
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if indexPath.row == manArray.count - 1 {
let tmpArray = ["Men","m1","m2","m3","m4","m5","m6","m7"]
let tmpArray2 = ["789","1259","959","1625","1259","936","980","1500"]
manArray.append(contentsOf: tmpArray)
mPrice.append(contentsOf: tmpArray2)
productListCell.reloadData()
}
}
}

这是我更新的代码

最佳答案

试试这个代码

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if indexPath.row == manArray.count - 1 {
let tmpArray = ["Men","m1","m2","m3","m4","m5","m6","m7"]
let tmpArray2 = ["789","1259","959","1625","1259","936","980","1500"]
manArray.append(contentsOf: tmpArray)
mPrice.append(contentsOf: tmpArray2)
yourCollectionView.reloadData()
}
}

关于swift - 如何在swift 4中的 Collection View 中实现字符串数组的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50998733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com