【问题标题】:CollectionView delegate methods are not calling from TableViewCellCollectionView 委托方法未从 TableViewCell 调用
【发布时间】:2020-01-26 08:10:54
【问题描述】:

没有从 tableViewCell 调用委托。这是UICollectionViewDelegate 和UICollectionViewDataSource 代码

extension HomeVC:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath)
        return cell

    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("ok")

    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lay = collectionViewLayout as! UICollectionViewFlowLayout
        lay.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        lay.minimumInteritemSpacing = 0
        lay.minimumLineSpacing = 0
        collectionView.collectionViewLayout = lay
        let size = (collectionView.frame.size.width-10) / 2
        return CGSize(width: size, height: size)
    }

}

【问题讨论】:

  • collection view的delegate设置是否正确?

标签: ios xcode uitableview uicollectionview swift4


【解决方案1】:

这是我在 TableViewCell 中使用 collectionView 完成相同操作并执行选择的代码:

TableView 数据源方法内的代码:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "BasicCarColorCell", for: indexPath) as! BasicCarColorCell
    cell.dataSource = preloads.colors
    cell.collectionViewSetup()
    cell.delegate = self
    return cell
}

TableViewCell 的代码:

protocol BasicCarColorCellDelegate {
func colorCell(cell:BasicCarColorCell, didSelect color: Color)
}

class BasicCarColorCell: UITableViewCell {
var dataSource = Array<Color>()
var selectedColor = Color()

@IBOutlet weak var textView: RSKPlaceholderTextView!

@IBOutlet weak var guideLineMessage:UILabel!

@IBOutlet weak var collectionView: UICollectionView!

var delegate: BasicCarColorCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

func collectionViewSetup() {
    let nib = UINib(nibName: "BasicCarColorCollectionCell", bundle: nil)
    self.collectionView.register(nib, forCellWithReuseIdentifier: "BasicCarColorCollectionCell")
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.minimumLineSpacing = 0
    flowLayout.minimumInteritemSpacing = 0
    flowLayout.scrollDirection = .horizontal
    collectionView.collectionViewLayout = flowLayout
    collectionView.dataSource = self
    collectionView.delegate = self
}

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

    // Configure the view for the selected state
}

}


extension BasicCarColorCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.dataSource.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BasicCarColorCollectionCell", for: indexPath) as! BasicCarColorCollectionCell

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let height = collectionView.bounds.size.height-2
    let width = height-20
    return CGSize(width: width, height:height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 1, left: 10, bottom: 1, right: 10)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let color = self.dataSource[indexPath.item]
    self.selectedColor = color
    delegate?.colorCell(cell: self, didSelect: self.selectedColor)
    collectionView.reloadData()
}

}

而要处理collectionView的选择,只需在ViewController中实现TableViewCell中编写的自定义协议方法即可:

func colorCell(cell: BasicCarColorCell, didSelect color: Color) {
    //self.selectedCarColor = color.value

}

您可以根据需要以相同的模式进行操作。

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    您需要为collectionView设置数据源和委托,每次在Tableview委托CellForRowAtIndexPath中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      相关资源
      最近更新 更多