对于不同的笔尖,collectionView单元格宽度不变

时间:2018-12-13 18:58:34

标签: ios swift nib collectionview

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

enter image description here

如果我按一下空格,则表示单击了加号单元格。

所有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没有更改大小,是否有人知道如何更改第一个单元格的宽度?谢谢

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

尝试使用UICollectionViewDelegateFlowLayout方法。在Xcode 11或更高版本中,您需要将Xib的估算大小设置为无。

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)
}