3

I am trying to display table view and once you select a row it shall expand to show a collection view grid of the products within that category. I've managed to get the table view cell expanding and displaying an image, by modifying the height constraint of the image (not the xib method, i tried, no success), following the tutorial below however I cannot seem to get it working if i insert a collection view.

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

This is what I am trying to achieve:

My code:

Custom Table view Cell Class

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

}

}

View controller

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

    }
7
  • Show some code .. what you already did .. what is your problem exactly Commented Apr 30, 2017 at 13:29
  • I have added the code for my VC and the custom table view cell. thanks Commented Apr 30, 2017 at 13:33
  • 1
    "I cannot seem to get it working" ... that's not a very good description of what you're having trouble with. What is not working? The app won't run? The app crashes? The app runs but displays picture of kittens when you want to display hamburgers? Commented Apr 30, 2017 at 13:37
  • when i try implementing a collection view inside the table view cell, the expanding part stops functioning so the table view cells contain the title but once tapped do not expand Commented Apr 30, 2017 at 14:09
  • So, when you have an ImageView, and you change the height constraint constant of the ImageView, it works as desired? But if you use a CollectionView in place of the ImageView, and you change the height constraint constant of the CollectionView, nothing happens? Are you sure you're assigning the constraints properly? Commented Apr 30, 2017 at 16:01

2 Answers 2

1

Existing Approach: I would suggest try using a section header(UITableViewHeaderFooterView). Once you tap on the section header you can set the number of rows to 1. Here you have to make sure the rowHeight = height of number of items in collection.

This would include too much of work.

Simplify the Approach. Use the collection view directly , with section header and layout configured for veritical Scroll.

Sign up to request clarification or add additional context in comments.

4 Comments

I think i'm going to try the "existing approach" method. In terms of making the header look like a row, can i increase the size of the header itself? And how would i register the tap on the header? sorry I'm quite new to coding. thanks in advance
I've increased the size of the header, however I can't seem to find a function to register a tap on the header itself, only the row. Any suggestions? @andyPaul
There is no function of header Selection.You can use a tap gesture recognizer.
or you can add a button which will have a target function?
0

CollectionViewCell.swift

import UIKit
class FriendCVCell: UICollectionViewCell {

    @IBOutlet weak var FriendNameLabel: UILabel!

    @IBOutlet weak var FriendMobileNOLabel: UILabel!
}

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.