在表格视图单元格swift 3中添加集合视图

时间:2017-04-30 13:27:02

标签: ios swift uitableview swift3 uicollectionview

我正在尝试显示表格视图,一旦您选择了一行,它将展开以显示该类别中产品的集合视图网格。我设法让表格视图单元格扩展并显示图像,通过修改图像的高度约束(不是xib方法,我试过,没有成功),按照下面的教程,但我似乎无法得到它如果我插入一个集合视图,我正在工作。

http://codepany.com/blog/swift-3-expandable-table-view-cells/

这就是我想要实现的目标:

我的代码:

自定义表格视图单元格类

class ExpandableCell: UITableViewCell {

@IBOutlet weak var view: UIView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var img: UIImageView!
@IBOutlet weak var imgHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var viewHeight: NSLayoutConstraint!

var isExpanded:Bool = false
{
    didSet
    {
        if !isExpanded {
            self.imgHeightConstraint.constant = 0.0

        } else {
            self.imgHeightConstraint.constant = 128.0
        }
    }
}

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


}

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

    // Configure the view for the selected state

}

}

查看控制器

class NewMenuVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
//    @IBOutlet weak var tableViewCell: UITableViewCell!
//    @IBOutlet weak var collectionView: UICollectionView!
//    @IBOutlet weak var collectionViewCell: UICollectionViewCell!

    let imgs = ["0", "1", "2", "0", "1", "2", "0", "1", "2", "0", "1", "2", "0", "1", "2", "0", "1", "2", "0", "1", "2"]
    let categories = ["Hamburgers", "Saver Menu", "Sides", "Desserts", "Drinks", "Offers", "Specials", "Wraps", "Chicken", "Meals"]

    var expandedRows = Set<Int>()

    //@IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {

        super.viewDidLoad()

        self.tableView.delegate = self

        self.tableView.dataSource = self

        self.tableView.rowHeight = UITableViewAutomaticDimension

    }


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

        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:ExpandableCell = tableView.dequeueReusableCell(withIdentifier: "ExpandableCell") as! ExpandableCell

        cell.img.image = UIImage(named: imgs[indexPath.row])

        cell.isExpanded = self.expandedRows.contains(indexPath.row)

        cell.label.text = categories[indexPath.row]

        return cell

    }


    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return 144.0

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print(indexPath.row)

        guard let cell = tableView.cellForRow(at: indexPath) as? ExpandableCell

            else { return }



        switch cell.isExpanded

        {

        case true:

            self.expandedRows.remove(indexPath.row)

        case false:

            self.expandedRows.insert(indexPath.row)

        }

        cell.isExpanded = !cell.isExpanded


        self.tableView.beginUpdates()

        self.tableView.endUpdates()

    }

2 个答案:

答案 0 :(得分:0)

现有方法: 我建议尝试使用节标题(UITableViewHeaderFooterView)。点击节标题后,您可以将行数设置为1。 在这里,您必须确保rowHeight =集合中项目数的高度。

这将包括太多的工作。

简化方法。 直接使用集合视图,并为veritical Scroll配置节标题和布局。

答案 1 :(得分:-1)

CollectionViewCell.swift

import UIKit

class MyPlanTVCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!

    @IBOutlet weak var PlanNameLabel: UILabel!

    @IBOutlet weak var MallNameLabel: UILabel!

    @IBOutlet weak var PlanDateLabel: UILabel!

    var FriendNameArray: NSArray = []

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        collectionView.delegate = self as! UICollectionViewDelegate
        collectionView.dataSource = self as! UICollectionViewDataSource
         FriendNameArray = ["Sunny", "Mike", "Tom", "Henry", "Jenny", "Neena", "Julia" ]

    }
}


extension MyPlanTVCell
: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return FriendNameArray.count

    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! FriendCVCell
        //What is this CustomCollectionCell? Create a CustomCell with SubClass of UICollectionViewCell
        //Load images w.r.t IndexPath
    cell.collectionImageView.image = UIImage(named:"default"[indexPath.row])

    cell.FriendNameLabel.text = FriendNameArray[indexPath.row] as! String


        cell.backgroundColor = UIColor.green



        //gameNames[indexPath.row]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let clickedIndex = 
            imageNames[indexPath.row]
        print(clickedIndex)
    }
}

tableCell.swift

{{1}}
相关问题