gpt4 book ai didi

ios - 从未调用过的 UICollectionViewDelegateFlowLayout 方法

转载 作者:可可西里 更新时间:2023-11-01 02:12:42 29 4
gpt4 key购买 nike

我有一个 UICollectionView,它位于 UITableViewCell 之上。单元格是 Collection View 的委托(delegate)和数据源。一切都在 Storyboard中设置。单元格实现了 numberOfItemsInSectioncellForItemAtIndexPath 方法,并且这些方法总是被成功调用。该单元格还实现了 UICollectionViewDelegateFlowLayout 协议(protocol)中的方法,例如 sizeForItemAtIndexPath。这些方法永远不会被调用。这可能是什么原因?

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
}
}

最佳答案

根据swift版本检查方法名:

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

=======> sizeForItemAtIndexPath

关于ios - 从未调用过的 UICollectionViewDelegateFlowLayout 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40547310/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com