I'm new to swift and I've been stuck on this for a while now I'm trying to pass a Struct Array from a tableview cell to another view controller and into another tableview. The cleans way possible, and the entire array, and I'm trying to avoid sending it individually and then adding to a new array in secondViewController. Also the problem I'm facing is I need to send whatever is in that cell. I'm doing this in UIkit and Storyboard Any help would be great, thank you in advice.
Data Models
struct Comment {
var name: String
var comment: String
}
struct Model{
var name: String
var post: String
var like: Int
var comments: [Comment]
}
First View Controller / Table View
class MainPage: UIViewController{
var getInfo: [Model] = [Model(name: "John Wick", post: "My Dog", like: 25, comments: [Comment(name: "Keanu", comment: "Yep I understand")])]
}
extension MainPage: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return getInfo.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "NewsFeedViewCell", for: indexPath) as? NewsFeedViewCell else {return
UITableViewCell()}
let info = getInfo[indexPath.row]
cell.nameLabel.text = info.name
cell.postLabel.text = info.post
cell.likeLabel.text = "\(info.like) Likes"
cell.commentsLabel.text = "\(info.comments.count) Comments"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "Comment") as? Comment else {return}
navigationController?.pushViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 210
}
}
SecondViewController / Table View
class Comment: UIViewController {
var newFeed = [Model]()
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension Comment: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newFeed.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CommentViewCell", for: indexPath) as? CommentViewCell else {return
UITableViewCell()}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 180
}
}
CodePudding user response:
If you want to shop specific cell's data into table of other view controller, then you need to create an object of Model struct rather than its array just like below:
var newFeed: Model?
Next you can assign it value of particular cell before navigation in main did select method as below:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "Comment") as? Comment else {return}
vc.newFeed = getInfo[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
It will assign that cell's data to newFeed Variable and relaod table with that data.
For any question, feel free to ask.