加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 为什么没有调用UICollectionView insetForSectionsAtIn

发布时间:2020-12-14 04:31:50 所属栏目:百科 来源:网络整理
导读:我正在寻找使用以下功能,但不幸的是它没有被调用. func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewFlowLayout,insetForSectionAtIndex section: Int) - UIEdgeInsets { let cellCount = Double(collec
我正在寻找使用以下功能,但不幸的是它没有被调用.

func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewFlowLayout,insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        let cellCount = Double(collectionView.numberOfItems(inSection: section))
        if let cell = collectionView.cellForItem(at: IndexPath(row: 0,section: section))
        {
            let cellWidth = Double(cell.frame.size.width)
            let totalCellWidth = cellWidth * cellCount
            let cellSpacing = Double(collectionViewLayout.minimumInteritemSpacing)
            let totalSpacingWidth = cellSpacing * (cellCount - 1)

            let leftInset = (collectionView.frame.width - CGFloat(totalCellWidth + totalSpacingWidth)) / 2
            let rightInset = leftInset

            return UIEdgeInsetsMake(0,leftInset,rightInset)
        }

    return collectionView.layoutMargins
}

我收到以下控制台错误,但我的单元格小于我的collectionView,并且我将section insets和layout margin设置为0.

2017-03-30 11:25:14.624 Prep[92400:1862856] The behavior of the UICollectionViewFlowLayout is not defined because:
2017-03-30 11:25:14.624 Prep[92400:1862856] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values,minus the content insets top and bottom values.
2017-03-30 11:25:14.624 Prep[92400:1862856] The relevant UICollectionViewFlowLayout instance is,and it is attached to ; layer = ; contentOffset: {0,0}; contentSize: {0,68.5}> collection view layout: .
2017-03-30 11:25:14.625 Prep[92400:1862856] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
cell height –> 42.0

enter image description here

协议

class OverviewViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var vipCollectionView: UICollectionView!

var hostedMeetingsArray: [String]?
var vipArray: [Attendee]?

class OverviewViewController: UIViewController,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var vipCollectionView: UICollectionView!

var hostedMeetingsArray: [String]?
var vipArray: [Attendee]?

enter image description here

解决方法

好吧,没有调用insetForSectionAt的问题是你必须像这样实现它:

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

   //access to minimumInteritemSpacing by casting to UICollectionViewFlowLayout
   let layout = collectionViewLayout as! UICollectionViewFlowLayout
   let spacing = layout.minimumInteritemSpacing       

   return UIEdgeInsetsMake(0.5,0.5,0)

}

看看UICollectionViewDelegateFlowLayout协议. insetForSectionAt声明如下:

@available(iOS 6.0,*)
optional public func collectionView(_ collectionView: UICollectionView,insetForSectionAt section: Int) -> UIEdgeInsets

所以你需要第一个可选参数来实现这个功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读