UICollectionView的数据函数未调用

时间:2018-11-23 17:46:02

标签: ios swift uicollectionview uikit

基本上,我在UICollectionView中有一个UIView(用于扩展集合视图数据和委托协议)。某些UICollectionView函数,例如numberofSectionsnumberOfItemsInSection被调用,而其他函数,例如cellForRowAt,则没有被调用。

有问题的集合视图为rolesCollectionView,其数据为roles数组。我添加了一些打印语句,以查看正在调用的函数,而只有上述函数被调用。我还可以肯定RolesCollectionViewCell并不是问题,如有必要,我也可以提供其代码。

class ProjectSearchViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

var rolesCollectionView: UICollectionView!
var roles = ["Developer", "Designer", "Backend Developer", "Frontend Developer"]

override func viewDidLoad() {
    super.viewDidLoad()
    print("test")
    // Do any additional setup after loading the view.
    navigationItem.title = "Search Projects"

    view.backgroundColor = .white

    var rolesLayout = UICollectionViewFlowLayout()
    rolesLayout.scrollDirection = .horizontal
    rolesLayout.minimumLineSpacing = 8
    rolesLayout.minimumInteritemSpacing = 8
    rolesLayout.sectionInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
    rolesLayout.estimatedItemSize = CGSize(width: 50, height: 25)

    rolesCollectionView = UICollectionView(frame: .zero, collectionViewLayout: rolesLayout)
    rolesCollectionView.backgroundColor = .white
    rolesCollectionView.delegate = self
    rolesCollectionView.dataSource = self
    rolesCollectionView.register(RolesCollectionViewCell.self, forCellWithReuseIdentifier: "role")
    rolesCollectionView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(rolesCollectionView)
    rolesCollectionView.reloadData()

    let newView = UIView()
    newView.backgroundColor = .black
    newView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(newView)
    NSLayoutConstraint.activate([
        newView.widthAnchor.constraint(equalToConstant: 50),
        newView.heightAnchor.constraint(equalToConstant: 50),
        newView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        newView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

    setupConstraints()

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   // if collectionView == self.rolesCollectionView {
    print("return")
    print(roles.count)
    return roles.count
   // } else {
      //  return 0
  //  }
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    print("section")
    return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    print("cell for item at")
    if collectionView == self.rolesCollectionView {
        var cell = rolesCollectionView.dequeueReusableCell(withReuseIdentifier: "role", for: indexPath) as! RolesCollectionViewCell
        cell.configure(roleName: roles[indexPath.row])
        print(roles[indexPath.row])
        return cell
    } else {
        return UICollectionViewCell()
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    //if collectionView == self.rolesCollectionView {
    print("size")
    print("test: \(collectionView == rolesCollectionView)")
    return CGSize(width: 15.0, height: 15.0)
  //  }
}

func setupConstraints() {
    NSLayoutConstraint.activate([
        rolesCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
        rolesCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
        rolesCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8)
        //rolesCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bot)
    ])
}

}

0 个答案:

没有答案