简体   繁体   中英

Table view in collection view

I have a UICollectionView with horizontal scrolling direction. It has a button. On that button click i want to show a list tableView . My question is where should i implement table view delegate method, in BasicVC, where i create collectionView ,

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let c = collectionView.dequeueReusableCell(withReuseIdentifier:"cell", for: indexPath)
    let btn = c.viewWithTag(9) as? UIButton
    btn?.layer.masksToBounds = true
    btn?.layer.cornerRadius = (btn?.frame.size.height)!/2
    btn?.backgroundColor = getRandomColor()
    return c
}

or in collectionCell class.

class PlayerHeaderCell: UICollectionViewCell{
@IBOutlet weak var btn: UIButton!}

tableView delegate methods needs to implemented in UICollectionViewCell like so

class PlayerHeaderCell: UICollectionViewCell,  UITableViewDataSource, UITableViewDelegate{


    @IBOutlet weak var btn: UIButton!
    @IBOutlet weak var tableView: UITableView!

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
        return cell
    }

}

It sounds like you want a table view to popup on top of the collection view like a drop down menu right? If you're wanting to have a table view that is displayed over top your current view, I'd recommend creating a new view controller for it in your story board that contains the table view, and then presenting that controller on top of the current view. The result would be a table view floating wherever you want on top of your collection view.

Unlike what the other answer said, do NOT make the collection cell the table view delegate, it just doesn't make sense unless you want a different table view for every cell, in which case you should do this and set the delegate to be the selected collection cell.

To implement the popup table view:

  1. Create a view controller in storyboard
  2. Create a new table view, place it INSIDE the new view controllers default view (the default view is used as the background but will be invisible.)
  3. Set view controller's presentation to be Over Content
  4. Set the new view controllers background view to be completely transparent by changing its color
  5. Ctrl drag from your previous view controller to your new view controller and create a modal segue, give it some identifier string "id".
  6. Call performSegue with the ID you set will cause the table view to appear as it is in the new view controller you created.

The advantage to this approach is that your table view can be designed in it's view controller and wont clutter up the original view controller in a storyboard. Just my thoughts, you seem to be doing things programatically so I'm not sure if it's suitable to your use case but I hope it helps.

It depends on where you are showing the tableview. I mean when you tap button, if you are navigating to a new controller with tableview, the delegate should be in the new controller. Or if you are showing the tableview in the same controller, the delegates will be in BasicVC. I think the collection cell has only the button. This is my understanding. Someone please correct if I am wrong.

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