无法在代码 iOS 中更改 CollectionViewCell 的大小



我有问题,我想在代码中更改CollectionViewCell的大小,但我的函数不起作用。这里的代码viewConroller,

import UIKit
class ViewController: UICollectionViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 99
    }
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        cell.textField.text=String(indexPath.row)
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let cellsAcross: CGFloat = 3
        let spaceBetweenCells: CGFloat = 1
        let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
        return CGSize(width: dim, height: dim)
    }
}

变化大小的功能是最后的

和此Cell代码

import UIKit
class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var textField: UITextField!
}

运行应用程序时,小区的大小不会更改,尽管我更改了值。

hi使用以下代码自定义集合查看单元格

extension SKHodDashBoardViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (UIScreen.main.bounds.size.width - 40) / 2
        return CGSize(width:width, height:75);
    }
}

首先,实现 UICollectionViewDataSource& UICollectionViewDelegate 替补函数在您的课内。

然后在viewDidLoad内部分配数据源并委派给您的收集视图:

self.dataSource = self
self.delegate = self

相关内容

  • 没有找到相关文章

最新更新