删除UICollectionView的单元格之间的空间

时间:2018-04-11 01:02:31

标签: swift uicollectionview rx-swift

UICollectionViewCell只有一个UIImageView。我正在使用RxSwift进行项目。 delegate设置Main.storyboarddatasource设置RxSwift。我需要删除单元格之间的空格,但有麻烦。此线程中的方法无效:

Aligning collection view cells to fit exactly 3 per row

我尝试了两个答案但没有任何反应。

以下是UIViewCollectionController s'代码:

let disposeBag = DisposeBag()

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    //self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.
    collectionView?.dataSource = nil
    setupCellConfiguration()
}

private func setupCellConfiguration() {
    //1
    immuneCells
        .bind(to: self.collectionView!
            .rx //2
            .items(cellIdentifier: CellTypeCollectionViewCell.Identifier,
                   cellType: CellTypeCollectionViewCell.self)) { // 3
                    row, cellType, cell in
                    cell.configureWithCellType(cellType: cellType) //4
        }
        .disposed(by: disposeBag) //5
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    let num = CellType.immune.count
    return num
}

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let screenWidth = UIScreen.main.bounds.width
    let scaleFactor = (screenWidth / 3) - 6

    return CGSize(width: scaleFactor, height: scaleFactor)
}

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

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

任何建议都将不胜感激

  

更新

我尝试设置相等的宽度和高度,UIImageView的顶部,左边约束,但它没有帮助。

它的外观如下:

enter image description here

3 个答案:

答案 0 :(得分:1)

从评论中可以看出,您的FlowLayout方法没有被调用。

UICollectionViewDelegateFlowLayout添加到UICollectionView的{​​{1}}定义行。

类似的东西:

class

这将告诉iOS你的class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 实现了该协议并支持布局方法,以便它知道调用它们。

答案 1 :(得分:0)

尝试

collectionView.minimumLineSpacing = 0
collectionView.minimumInteritemSpacing = 0

答案 2 :(得分:0)

您应该使用UICollectionViewFlowLayout。 以下是示例代码:

var flowLayout = UICollectionViewFlowLayout()
let numberOfItemsPerRow = 3
let itemSpacing = 5.0
let lineSpacing = 5.0

let width = ((self.collectionView?.width)! - itemSpacing*(numberOfItemsPerRow+1)) / numberOfItemsPerRow
flowLayout.itemSize = CGSize(width: width, height: width)
flowLayout.minimumInteritemSpacing = itemSpacing
flowLayout.sectionInset = UIEdgeInsetsMake(lineSpacing, lineSpacing, lineSpacing, lineSpacing)
self.collectionView?.collectionViewLayout = flowLayout