TableView内部水平滚动集合视图单元格

时间:2017-10-06 15:29:32

标签: swift tableview collectionview

您好我在水平滚动集合视图中有一个tableview,如下所示。

http://recordit.co/pUyTbuOPsn

TableView单元格由tvdatasource正确填充但是 由于TableView嵌入在一个CollectionViewCell方法中,例如didselectitematindexpath没有被调用。

如何使用控制器和集合视图单元正确设置tableviewdelegate方法,以便委托方法可以汇集到顶层控制器?

它很容易填充但是调用方法是我需要帮助的:D 这是下面的代码

此外,我将在整个应用程序中重复使用此模式。许多表格视图不会是静态的,数据将从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() {
}

}

0 个答案:

没有答案