在单个UIViewController中没有为多个集合视图调用cellForItemAtIndexPath indexPath

时间:2016-03-31 14:31:38

标签: ios swift uiviewcontroller uicollectionview uicollectionviewcell

我正在尝试在单个视图控制器中拥有多个集合视图。我试图通过为每个集合视图使用单独的标记来实现这一点,但在我看来,celForItemAtIndexPath没有被调用。以下是我UIViewController的代码。

import UIKit
class HomeViewController: UIViewController {
  // MARK: - IBOutlets

  @IBOutlet weak var backgroundImageView: UIImageView!
  @IBOutlet weak var collectionView: UICollectionView!
  @IBOutlet weak var friendsCollectionView: UICollectionView!

  // MARK: - UICollectionViewDataSource
  private var interests = Interest.createInterests()
  private var friends = Friends.createFriends()

  override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.tag = 100;
    self.friendsCollectionView.tag = 200;
    collectionView.registerClass(NSClassFromString("InterestCell"),forCellWithReuseIdentifier:"cell");
    friendsCollectionView.registerClass(NSClassFromString("FriendCell"),forCellWithReuseIdentifier:"cellB");

    self.view.addSubview(collectionView)
    self.view.addSubview(friendsCollectionView)
    print("view did load")
  }

  private struct Storyboard {
    static let CellIdentifier = "InterestCell"
    static let FriendIdentifier = "FriendCell"
  }
} 

extension HomeViewController : UICollectionViewDataSource {
  func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    print("number of sections are 1")
    return 1
  }

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    print("trying this")
    if collectionView.tag == 100 {
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.CellIdentifier, forIndexPath: indexPath) as! InterestCollectionViewCell

      print("inside pluto")
      return cell
    }
    else {
      let cellB = friendsCollectionView.dequeueReusableCellWithReuseIdentifier(Storyboard.FriendIdentifier, forIndexPath: indexPath) as! FriendListCollectionViewCell
      print("inside venus")
      return cellB
    }
  }

  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("hello")
    if collectionView == collectionView {
      print(interests.count)
      return interests.count
    }
    else {
      print("hmmm")
      return friends.count 
    }
  }
}

有人可以建议我做错了吗?

3 个答案:

答案 0 :(得分:1)

要访问cellForItemAtIndexPath方法,您必须设置collectionView数据源,如:

collectionView.dataSource = self;
friendsCollectionView.dataSource = self;

此外,最好不要使用标签来识别每个集合,使用类似于:

的内容
if (collectionView == self.collectionView) { } else if (collectionView
== self.friendsCollectionView) { // init friend cell ... }

答案 1 :(得分:0)

您应该指定集合的​​委托和dataSource。

collectionView.delegate = self
collectionView.dataSource = self

请务必将UICollectionViewDelegateUICollectionViewDataSource添加到ViewController。

答案 2 :(得分:0)

  1. 您是否正确设置了数据源和代理?由于该课程不符合UICollectionViewDataSourceUICollectionViewDelegate,我猜不是。在viewDidLoad中执行此操作:

    collectionView.delegate = self collectionView.dataSource = self friendsCollectionView.delegate = self friendsCollectionView.dataSource = self

  2. 或通过xib来做。

    1. 通过编辑类声明使类符合UICollectionViewDataSourceUICollectionViewDelegate

      类HomeViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource

    2. 我建议您使用对象的引用而不是标记。通过检查委托的collectionView对象是否与您实例化的对象相同来参考集合视图:

      如果collectionView == self.friendsCollectionView {//做某事} 如果collectionView == self.collectionView {//做某事}