【问题标题】:cellForItemAtIndexPath indexPath not called for multiple collection views in a single UIViewController单个 UIViewController 中的多个集合视图未调用 cellForItemAtIndexPath indexPath
【发布时间】:2016-03-31 14:31:38
【问题描述】:

我试图在单个视图控制器中拥有多个集合视图。我试图通过为每个集合视图使用单独的标签来实现这一点,但在我看来,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 
    }
  }
}

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

【问题讨论】:

  • 您是否为两者正确设置了数据源和 collectionView 委托?

标签: ios swift uiviewcontroller uicollectionview uicollectionviewcell


【解决方案1】:

要到达 cellForItemAtIndexPath 方法,您必须像这样设置 collectionView 数据源:

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

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

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

【讨论】:

    【解决方案2】:

    您应该指定集合的​​委托和数据源。

    collectionView.delegate = self
    collectionView.dataSource = self
    

    记得将 UICollectionViewDelegateUICollectionViewDataSource 添加到您的 ViewController。

    【讨论】:

      【解决方案3】:
      1. 您是否正确设置了数据源和委托?由于该课程不符合UICollectionViewDataSourceUICollectionViewDelegate,我猜不。在viewDidLoad 中执行此操作:

        collectionView.delegate = self collectionView.dataSource = 自我 FriendsCollectionView.delegate = self FriendsCollectionView.dataSource = 自我

      或者通过xib来做。

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

        HomeViewController 类:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource

      2. 我建议您使用对对象的引用而不是标签。通过检查代表的 collectionView 对象是否等于您实例化的对象来引用集合视图:

        如果 collectionView == self.friendsCollectionView { //做点什么 } if collectionView == self.collectionView { //做点什么 }

      【讨论】:

        猜你喜欢
        • 2016-04-23
        • 2015-04-29
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多