如何在Swift 4的集合视图中实现字符串数组的分页

时间:2018-06-23 06:55:35

标签: swift string uicollectionview

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()
        }
    }
}

这是我的更新代码

1 个答案:

答案 0 :(得分:0)

尝试此代码

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()
        }
    }