简体   繁体   中英

CollectionView delegate methods are not calling from TableViewCell

截屏

Delegate isn't being called from tableViewCell. Here is UICollectionViewDelegate and UICollectionViewDataSource code

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

}

You need to set datasource and delegate for collectionView, every time in Tableview delegate CellForRowAtIndexPath.

Here is the code how I done for same inside TableViewCell using collectionView and performed selection:

Code inside TableView datasource method:

    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
}

Code for 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()
}

}

And to handle the selection of collectionView just implement the method of custom protocol written in TableViewCell in ViewController:

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

}

You can do it in same pattern as per your need.

Hope it'll help!

swift 5
Hope this help  you code

//MARK:- Tableview Datasource method 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCell(withIdentifier:"RecentLoanApplicationCell") as! RecentLoanApplicationCell
                           
                cell.collectionVC.delegate = self
                cell.collectionVC.dataSource = self
                cell.btnViewAll.addTarget(self, action: #selector(btnViewALLTap(button:)), for: .touchUpInside)
                DispatchQueue.main.async {
                               
                                cell.collectionVC.reloadData()
                 }
                return cell  
}

//MARK:- Tableview Cell in side custom cell Register for collectionView cell

class RecentLoanApplicationCell: UITableViewCell {

@IBOutlet  var collectionVC:UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()

    self.collectionVC.register(UINib.init(nibName:"RecentLoanCollectionViewCell", bundle: nil), forCellWithReuseIdentifier:"RecentLoanCollectionViewCell")
    
    
    
    }
override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}

}

// Same ControllerView  call  
//MARK:- Collection DataSource & Delegate

extension HomeViewController : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
            return 5   
    }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {  
    
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"RecentLoanCollectionViewCell", for: indexPath) as! RecentLoanCollectionViewCell
        
         return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)    {

        debugPrint("didSelectItemAt ==>\(indexPath.row)")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM