1

我有一个UICollectionView,它位于UITableViewCell上。单元格是集合视图的委托和数据源。一切都安装在故事板中。该单元实现了numberOfItemsInSectioncellForItemAtIndexPath方法,并且这些方法总是被成功调用。该单元还实现了UICollectionViewDelegateFlowLayout协议的方法,例如sizeForItemAtIndexPath。而这些方法从未被调用过。这可能是什么原因?根据SWIFT版本UICollectionViewDelegateFlowLayout方法从未调用

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

你能提供的代码? –

+0

@EmrahAkgül当然。你想看到什么代码? –

+0

您的自定义TableViewCell类。 –

回答

1

检查方法名称:

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

=======> sizeForItemAtIndexPath