UICollectionViewDelegateFlowLayout方法从未调用过

时间:2016-11-11 11:43:04

标签: ios swift cocoa-touch uicollectionview uicollectionviewlayout

我有UICollectionView位于UITableViewCell。单元格是集合视图的委托和数据源。一切都在故事板中设置。该单元格实现numberOfItemsInSectioncellForItemAtIndexPath方法,并且这些方法始终成功调用。此外,该单元还实现UICollectionViewDelegateFlowLayout协议的方法,例如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
}
}

1 个答案:

答案 0 :(得分:1)

根据swift版本检查方法名称:

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

=======> sizeForItemAtIndexPath