5

Hello I have a tableview inside horizontal scrolling collection view as shown below.

http://recordit.co/pUyTbuOPsn

TableView cells get populated properly by tvdatasource but since TableView is embedded in a CollectionViewCell methods such as didselectitematindexpath does not get called.

how can I properly setup tableviewdelegate methods with controller and collection view cell so that delegate methods can funnel through to the top controller?

its easy to populate but CALLING the methods are what I need help with :D here is the code below

also I will be reusing this pattern all over the app. many tableviews won't be static and data will be dynamically fed from the API.

class AddItemViewController: UIViewController, 
UICollectionViewDelegate, UICollectionViewDataSource, 
UICollectionViewDelegateFlowLayout
    {

@IBOutlet weak var mainCollectionView: UICollectionView!
var currentCellPosition = 0

override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.tintColor = UIColor.leafBlack()

    automaticallyAdjustsScrollViewInsets = false
    mainCollectionView.dataSource = self
    mainCollectionView.delegate = self

    mainCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    mainCollectionView.register(AddItemDetailsCollectionViewCell.self, forCellWithReuseIdentifier: "DetailsCell")
    mainCollectionView.register(AddItemPhotoCollectionViewCell.self, forCellWithReuseIdentifier: "PhotoCell")
    mainCollectionView.register(AddItemShippingCollectionViewCell.self, forCellWithReuseIdentifier: "ShippingCell")

    if let flowLayout = mainCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.scrollDirection = .horizontal
        flowLayout.minimumLineSpacing = 0
        flowLayout.itemSize = mainCollectionView.frame.size
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets.zero
        flowLayout.footerReferenceSize = CGSize.zero
        flowLayout.headerReferenceSize = CGSize.zero
    }
    mainCollectionView.isPagingEnabled = true
}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)  -> Int {
    return 3
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

    if(indexPath.row == 0) {
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DetailsCell", for: indexPath) as! AddItemDetailsCollectionViewCell
    }else if(indexPath.row == 1) {
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! AddItemPhotoCollectionViewCell
    }else if(indexPath.row == 2) {
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShippingCell", for: indexPath) as! AddItemShippingCollectionViewCell
    }

    return cell
}

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


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize.init(width: view.frame.width, height: view.frame.height)
}

@IBAction func showPrevCell(_ sender: Any) {
    if (currentCellPosition > 0) {
        currentCellPosition -= 1
        mainCollectionView.scrollToItem(at: IndexPath.init(row: currentCellPosition, section: 0), at: .centeredHorizontally, animated: true)
    }
}
@IBAction func showNextCell(_ sender: Any) {
    if (currentCellPosition < 2) {
        currentCellPosition += 1
        mainCollectionView.scrollToItem(at:IndexPath.init(row: currentCellPosition, section: 0), at: .centeredHorizontally, animated: true)
    }

}

}

import Foundation
import UIKit

class AddItemDetailsCollectionViewCell: BaseAddItemCollectionViewCell {

@IBOutlet weak var cellTitleLabel: UILabel!
@IBOutlet weak var mainTableView: UITableView!

var detailFieldNames = ["Model Name", "Brand", "Size", "Description", "Condition"]
var containingVC: AddItemViewController?

override var nibName: String! {
    get {
        return "AddItemDetailsCollectionViewCell"
    } set {}
}


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    xibSetup()
    configView()
}


override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
    configView()
}

override func configView() {
    mainTableView.dataSource = self
    mainTableView.delegate = self
    mainTableView.separatorStyle = .none
    mainTableView.allowsSelection = false


    let attributes = [NSFontAttributeName: UIFont(name: "Rokkitt-Thin", size: 22)!, NSForegroundColorAttributeName: UIColor.leafBlack(), NSKernAttributeName : 3.0] as [String : Any]
    cellTitleLabel.attributedText = NSAttributedString(string: "Detail Info", attributes: attributes)

    mainTableView.register(AddDetailTableViewCell.self, forCellReuseIdentifier: "Cell")
}

override func draw(_ rect: CGRect) {
    super.draw(rect)

}

}

extension AddItemDetailsCollectionViewCell: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! AddDetailTableViewCell
    cell.checkBox.setCheckState(.mixed, animated: true)

    //
}

}

extension AddItemDetailsCollectionViewCell: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? AddDetailTableViewCell {
        cell.setupTitleLabel(detailFieldNames[indexPath.row])

        return cell
    }

    return UITableViewCell()
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

}

class BaseAddItemCollectionViewCell: UICollectionViewCell {

var cell: UICollectionViewCell!
var nibName: String!

override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
    configView()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    xibSetup()
    configView()
}

func xibSetup() {
    do {
        try cell = loadCellFromNib()

        // use bounds not frame or it'll be offset
        cell.frame = bounds

        // Make the view stretch with containing view
        cell.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        // Adding custom subview on top of our view (over any custom drawing > see note below)
        addSubview(cell)
    } catch {
        print("Failed to create view from nib")
    }
}

func loadCellFromNib() throws -> UICollectionViewCell {

    let bundle = Bundle.main
    let nib = UINib(nibName: nibName, bundle: bundle)
    let cell = (nib.instantiate(withOwner: self, options: nil)[0] as? UICollectionViewCell)!

    return cell
}

func configView() {
}

}

Yoon Kang
  • 61
  • 4

0 Answers0