Andrey Chernukha Andrey Chernukha - 7 days ago 8
iOS Question

UICollectionViewDelegateFlowLayout methods never called

I have a

UICollectionView
which lies upon a
UITableViewCell
. The cell is the delegate and datasource for the collection view. Everything is setup in a storyboard. The cell implements
numberOfItemsInSection
and
cellForItemAtIndexPath
methods and these methods are always called successfully. Also the cell implements methods from
UICollectionViewDelegateFlowLayout
protocol such as
sizeForItemAtIndexPath
. And these methods are never called. What may be a possible reason for that?

class SignUpFollowCategoriesCell : UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{

@IBOutlet weak var categoryCollection: UICollectionView!
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var followButton: UIButton!

func setChosen(chosen:Bool)
{
followButton.checked = chosen
}

var category:Category?
{
didSet
{
guard let confirmedFollowButton = self.followButton else
{
return
}
confirmedFollowButton.hidden = true
guard let category = self.category else
{
return
}
guard let controller = self.controller else
{
return
}
guard let sites = controller.allSites[category.id] else
{
return
}
confirmedFollowButton.hidden = sites.count == 0
}

}
weak var controller:SignUpFollowCategoriesController?

func setupWithController(control:SignUpFollowCategoriesController)
{
controller = control
guard let tmp = followButton else
{
return
}
tmp.addTarget(self, action: #selector(SignUpFollowCategoriesCell.clicked), forControlEvents: .TouchUpInside)
followButton.setStyleOptions(false, style: .ClearBackground, title: "+ Follow All")
followButton.setStyleOptions(true, style: .YellowBackground, title: "Following")

}

func clicked()
{
guard let control = controller else
{
return
}
guard let cat = category else
{
return
}
if control.categoryIsChosen(cat)
{
self.setChosen(false)
control.unfollowCategory(cat)
}else
{
self.setChosen(true)
control.followCategory(cat)
}
categoryCollection.reloadData()

}

private func updateFollowBtnStyle()
{
guard let control = controller else
{
return
}
guard let cat = category else
{
return
}
if control.categoryIsChosen(cat)
{
self.setChosen(true)
}else
{
self.setChosen(false)
}
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if let tmp = self.category
{
if let sites = controller?.allSites[tmp.id]
{
return sites.count
}
}

return 0;
}


private func firstLetterCaps(str : String) -> String
{
var ret = str
ret.replaceRange(str.startIndex...str.startIndex, with: String(str[str.startIndex]).capitalizedString)
return ret
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{

if let cell:SignupCategorySiteCell = collectionView.dequeueReusableCellWithReuseIdentifier("SignUpCategoryCollectionCell", forIndexPath: indexPath) as? SignupCategorySiteCell
{
cell.imageView.image = UIImage.imageWithColor(UIColor.slangBlueLight())
var slangSite : SlangSite?

if let tmpID = self.category?.id
{
let sites = controller?.allSites[tmpID]

if let site = sites![indexPath.row] as SlangSite?
{
if let url = site.details.siteProfileImageURL()
{
cell.imageView.af_setImageWithURL(url)
}

let siteName = firstLetterCaps(site.details.siteName)
cell.siteNameLabel.text = siteName

slangSite = site
}
}
if let control = controller,let site = slangSite
{
if control.siteIsChosen(site)
{
cell.followStyle()
}
else
{
cell.unfollowStyle()
}
}
cell.onCellClickedBlock = {[weak self,controller] in
if let site = slangSite, let control = controller
{
if control.siteIsChosen(site)
{
cell.unfollowStyle()
control.unfollowSite(site)
}
else
{
cell.followStyle()
control.followSite(site)
}
self?.updateFollowBtnStyle()
}
}
return cell
} else
{
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "DebugCell")
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("DebugCell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.greenColor()
return cell
}
}

func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(92, 112)
}

func collectionView( collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsZero
}

func collectionView( collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 100.0
}
}

Answer

Check method name according to the swift version:

public func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize

=======> sizeForItemAtIndexPath