【发布时间】: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