首页 > 解决方案 > 不同笔尖的collectionView单元格宽度不会改变

问题描述

我有一个嵌套在表格视图单元格内的集合视图。集合视图中的第一个单元格是一个允许用户创建新列表的按钮。它的宽度小于主列表单元格的宽度。我最近更改了代码,以便 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没有改变大小?谢谢

标签: iosswiftnibcollectionview

解决方案


您忘记添加此协议: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)
}

推荐阅读