UICollectionView inside UITableViewCell how to set the selection to pass data from the current view to another one
You have two choice
1) Implement Collection view delegate & datasource inside view controller. You can do this in tableview's cellForRowAt method like cell.collectionview.datasource = self
2) You can create delegate / closure to pass data from Collection View -> TableView Cell --> UIViewController
Hope it is helpful And let me know if you need example
EXAMPLE
As you have finding difficulties to understand theoretically here is example
suppose in your view controller
You have tableview datasource like below
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"identifer", for: indexPath) as! YourTableViewCell
// You have collection view inside tableview cell so
cell.yourCollectionViewObject.dataSource = self
cell.yourCollectionViewObject.delegate = self
return cell
}
Implement or Move the datasource & Delegate in Your view Controller and push or present view controller you want to
Action to UITableView row inside UITableViewCell to open another view controller
TableViewCell cannot present a ViewController,
If your tableViewCell contain another tableView, and you click on one of its cell, you have to inform your ViewController that the cell was pressed
Put this in your tableview cell (which is inside another table view cell) didSelectRowAt method
NotificationCenter.default.post(name: Notification.Name("aNotificationName"), object: nil)
Add observer in you viewController viewDidLoad method
NotificationCenter.default.addObserver(self, selector: #selector(handleNotificationFuncName, name: NSNotification.Name(rawValue: "aNotificationName"), object: nil)
And handle your notification here
func handleNotificationFuncName(_ notification: Notification) {
let storyboard = UIStoryboard(name: "StoryboardName", bundle: Bundle.main)
if let vc = storyboard.instantiateViewController(withIdentifier: "pdfDisplayViewController") as? pdfDisplayViewController {
present(vc, animated: true, completion: nil)
}
}
Navigate to view controller from Tableview cell inside collectionview
You have following options
1) Implement tableview datasource and delgate in viewController instead of collection view cell
2) Use Delegate (explained below )
3) Use Closures
4) Use NotificationCenter
You need to create delegate or protocol as collection view cell can't push or present view controller.
Here is simple example (This is not exact code you may need modification)
Create protocol
protocol TableViewInsideCollectionViewDelegate:class {
func cellTaped(data:IndexPath)
}
Inside your collectionview cell add weak property
weak var delegate:TableViewInsideCollectionViewDelegate?
Now in your ViewController class you in cellForItem method of collectionview
you need to set delegate to self
like
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCell", for: indexPath) as! CustomCollectionCell
cell.delegate = self
return cell
and implement delegate method in viewController class and write code to push your view controller from there like self.navigationController.push
Now In Goto Collectionview Cell method
and whenever your tableviewDidSelect called
call delegate method like self.delegate?.cellTaped(data: dataYouWantToPass)
Hope it is helpful
Presenting a ViewController from within a CollectionViewCell that is nested in a TableViewCell
Use protocol and delegate like this
In tableViewCell
protocol CellDelegate {
func colCategorySelected(_ indexPath : IndexPath)
}
var delegate : CellDelegate?
In didSelect
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.colCategorySelected(indexPath)
}
In your ProfileViewController
class HomeVC: UIViewController , UITableViewDelegate, UITableViewDataSource, CellDelegate{
:
:
func colCategorySelected(_ indexPath : IndexPath){
// Push here
}
:
:
}
And dont forget
cellForRow
let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! tableCell
Cell.delegate = self // dont forget this line.
return Cell
How to push a view from inside a UITableViewCell that is in a UICollectionViewCell?
Get parent controller of current View -
//MARK: get parent controller...
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
Usage:-
Get navigationController reference as parentViewController?.navigationController
let viewController = GymLearnMoreViewController(nibName: "GymLearnMoreViewController", bundle: nil) //Your View controller instance
parentViewController?.navigationController?.pushViewController(viewController, animated: true)
Source Apple:-
https://developer.apple.com/documentation/uikit/uiresponder/1621099-next
https://developer.apple.com/documentation/uikit/uiresponder?changes=_9
Related Topics
I Want to Switch Time Scrubber to Do 15 Min Intervals
Issue with Encoding "&" in Urls
Swift: Extracting/Downcasting Cftype Based Coretext Types in a Cfarray
If Let Doesn't Unwrap Optional Value for Mkannotation's Title Property
How to Transfer Data Between Parent and Child View Controllers
Swift Converting Signed Array of Int [Int8] to Unsigned Array of Int [Uint8]
How to Design an Uiscrollview in Storyboard
How to Insert a Override Function into a If Else Statement
Center Horizontal Uicollectionview Based on Content
Checking If Text Fields Are Empty Cause Error in Swift 2
Saving an Image to Photos Library Using Swift 2.0
Uitabbar Transition Issue Below iOS 11 Swift
Why Clusterannotationformemberannotations in Mkmapview Is Not Called