Reputation: 505
This is giving me a very hard time. I am trying to segue from a collection view cell to a table view controller. When I click on a cell (specifically a button within a cell) I should be able to segue to the table view controller and have details of the cell clicked within the table view controller's cells.
In my view controller with the collection view cells, I have implemented a segue.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {
if segue.identifier == "UsersProfile" {
if let indexPath = sender as? IndexPath{
let vc = segue.destination as! UsersProfileViewController
let post = self.posts[indexPath.row] as! [String: AnyObject]
let posted = self.posts[indexPath.row] as! [NSMutableArray: AnyObject]
let username = post["username"] as? String
let userpicuid = post["uid"] as? String
let userpostuid = posted["uid"] as? NSMutableArray
vc.username = username
vc.userpicuid = userpicuid
vc.posts = userpostuid
print(indexPath.row)
}
}}
When I segue, the userpic and username appear in the detailed table view just fine but they are not viewed within the table view cells. This is my code for the table view controller:
var posts = NSMutableArray()
@IBOutlet var TableView: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UsersCell", for: indexPath) as! UserProfileTableViewCell
//Configure the cell
print(posts[indexPath.row])
let post = self.posts[indexPath.row] as! [String: AnyObject]
cell.Title.text = post["title"] as? String
cell.Author.text = post["Author"] as? String
cell.Price.text = post["Price"] as? String
if let imageName = post["image"] as? String {
let imageRef = FIRStorage.storage().reference().child("images/\(imageName)")
imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
if error == nil {
}else {
print("Error downloading image:" )
}})}
return cell
}
Nothing shows up in the table view cell. I am new to swift and I have been trying so hard to find a solution to this but I can't.
Heres a picture of my database:
Upvotes: 0
Views: 485
Reputation: 1668
I have created a sample that will navigate you to the tableViewController
when user will tap on UICollectionViewCell
.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// Change it with your required items.
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YOUR_CELL_IDENTIFIER", for: indexPath as IndexPath)
// Do not forgot to set cell tag.
cell.tag = indexPath.row//cell tag has been set to indexpath.row
return cell
}
In order to navigate to your tableViewController
implement PrepareForSegue
like mentioned below.
override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
if segue.identifier == "UsersProfile" {
// Use this tag to pass data for the selected cell
//this tag will act as indexpath.row
guard let tag = (sender as? UIView)?.tag
else {
return
}
// add the data you want to parse here.
print(tag)
}
}
NOTE: Make sure that you have set the segue in your storyboard file from UICollectionViewCell
to your UITableViewController
.
In case you required further details, please let me know i will add it to the answer.
Upvotes: 0
Reputation: 1176
Did you add the Delegate and Datasource of you tableview to the class?
Upvotes: 1