【问题标题】:collectionView cell width not changing for different nib不同笔尖的collectionView单元格宽度不会改变
【发布时间】:2018-12-13 18:58:33
【问题描述】:

我有一个嵌套在表格视图单元格内的集合视图。集合视图中的第一个单元格是一个允许用户创建新列表的按钮。它的宽度小于主列表单元格的宽度。我最近更改了代码,以便 collectionView 委托和 dataSource 方法位于 tableViewController 而不是表格视图单元格中,但是现在第一个单元格的宽度没有像移动代码之前那样改变。这张图显示:

如果我按下空白区域,则表明我单击了加号单元格。

所有 collectionView 委托和 dataSource 方法的代码:

extension ProfileTableViewController: UICollectionViewDelegate, UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print("the media: \(usersLists.count)")
    return usersLists.count + 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.item == 0 {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addNewListCell", for: indexPath) as! addNewListCell

        cell.currentUser = currentUser

        return cell

    }else{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! userListsCell

        cell.layer.applySketchShadow()
        cell.layer.cornerRadius = 20
        cell.layer.masksToBounds = false

        cell.currentUser = currentUser
        cell.media = usersLists[indexPath.item-1]

        return cell
    }

}



func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
    if indexPath.item == 0 {

        let size = CGSize(width: 80, height: 158)
        return size

    }else{

        let size = CGSize(width: 158, height: 158)
        return size

    }
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.item == 0 {
        //button
        self.addListDelegate?.addNewItem()
    }
}

我测试了更改sizeForItemAt 中大小的任何值是否会改变单元格大小,而任何单元格都不会发生任何变化

ProfileTableViewController 类中的表格视图方法,而不是扩展:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath) as! profileCell

    cell.selectionStyle = .none

    return cell

}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    guard let tableViewCell = cell as? profileCell else { return }

    tableViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.row) //this line sets the collectionView delegate and datasouce so that I can have all of the collectionView methods in this class

}

这是tableViewCell里面的代码

protocol addListCollectionDelegate: class {
    func addNewItem()
}

class profileCell: UITableViewCell, UICollectionViewDelegate {    

    @IBOutlet weak var collectionView: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()

        collectionView.register(UINib(nibName: "usersLists", bundle: nil), forCellWithReuseIdentifier: "listCell")
        collectionView.register(UINib(nibName: "newListCell", bundle: nil), forCellWithReuseIdentifier: "addNewListCell")

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func setCollectionViewDataSourceDelegate
        <D: UICollectionViewDataSource & UICollectionViewDelegate>
        (dataSourceDelegate: D, forRow row: Int) {

        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.reloadData()
    }

}

有谁知道如何更改第一个单元格的宽度,因为sizeForItemAt 没有更改大小?谢谢

【问题讨论】:

    标签: ios swift nib collectionview


    【解决方案1】:

    您忘记添加此协议:UICollectionViewDelegateFlowLayout

    当您想要返回一个项目的自定义尺寸时,它是必需的。

    还可以像下面这样改进您的代码:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        if indexPath.item == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addNewListCell", for: indexPath) as! addNewListCell
            cell.currentUser = currentUser
            return cell
        }
    
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! userListsCell
        cell.layer.applySketchShadow() // Write this line in cell class
        cell.layer.cornerRadius = 20 // Write this line in cell class
        cell.layer.masksToBounds = false // Write this line in cell class
        cell.currentUser = currentUser
        cell.media = usersLists[indexPath.item - 1]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        return indexPath.item == 0 ? CGSize(width: 80, height: 158) : CGSize(width: 158, height: 158)
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用 UICollectionViewDelegateFlowLayout 方法。在 Xcode 11 或更高版本中,您需要在 Xib 中将 Estimate Size 设置为 none。

      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: 
      UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
         let padding: CGFloat =  170
         let collectionViewSize = advertCollectionView.frame.size.width - padding
         return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
      }
      

      【讨论】:

      • 如何将估计大小准确设置为无?
      • 从情节提要中选择集合视图,然后转到情节提要左侧栏上的尺寸检查器(缩放图像选项),您将在第二行看到估计尺寸选项,您可以将估计尺寸设置为无、自动或自定义。
      猜你喜欢
      • 2017-11-08
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多