ios - 如何删除 UICollectionView 中单元格的空间

标签 ios swift xcode

我正在使用 UICollectionView。在 Collection View > Size Inspector > Min Spacing 中,我更改为 01

我想删除任何单元格之间的任何空间。 我该怎么办?

enter image description here

如何在不编写任何代码(空白)的情况下删除两个单元格之间的空格?

class FirstPageViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate
....   
    let reuseIdentifier = "cellFirstPage"
    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48","49","50"]


    // MARK: - UICollectionViewDataSource protocol

    // tell the collection view how many cells to make
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }

    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! ViewHolderFirstMainControllerCollectionViewController

        // Use the outlet in our custom class to get a reference to the UILabel in the cell

        print(self.items[indexPath.item])
        cell.lbl_title.text = self.items[indexPath.item]
        cell.backgroundColor = UIColor.cyan // make cell more visible in our example project

        return cell
    }

    // MARK: - UICollectionViewDelegate protocol

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
    }

    func collectionView(collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 1.0
    }

如果我不能不使用写代码,请写swift代码。

最佳答案

您必须增加单元格宽度才能消除单元格之间的间距。或者您可以在一行中创建 3 个单元格。尝试

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: view.bounds.width/2 - 2, height: *yourcustomheightforcell*)
}

关于ios - 如何删除 UICollectionView 中单元格的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42273650/

相关文章:

ios - 为什么页面将 UIToolBar 置于顶部?

ios - 如何停止 XCode 中 DerivedData 下的模块文件中的 'signature mismatch' 错误?

ios - 检索我们处于生命周期 Controller 的哪个状态

xcode - Xcode项目中缺少应用程序沙箱功能

objective-c - 国际化:在模拟器中只显示键名而不是本地化字符串

IOS UiTableView Cells 我怎样才能让它的高度可调

objective-c - 使用Core Graphics在Objective-C(IOS)中绘制角度/角度/"Conical"/"Arcing"渐变

ios - 对 UIButton 的更改在滚动之前不会更新单元格

iOS 13 状态栏故障

xcode - 仅限 iPad 方向纵向