I am trying to pass data when select or press cell in TableView, it’s working and I can print the result while select that cell.
The problem when the second view shows the data is nil.
her is the code when when select cell:
var feedItems: NSArray = NSArray()
var selectedPlayer : UsersModel = UsersModel()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Retrieve cell
let cellIdentifier: String = "BasicCell"
let myCell: WinnerTableCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! WinnerTableCell
// Get the location to be shown
let item: UsersModel = feedItems[indexPath.row] as! UsersModel
// Get references to labels of cell
myCell.lbTextName!.text = item.name
myCell.lbScore!.text = item.score
let imageURL = URL(string: "https://mywebsite.com/image-upload/img/(item.userImage ?? "nil")")
myCell.userImage.sd_setImage(with: imageURL, placeholderImage: UIImage(named: "nullImageQuestion.png"))
return myCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedPlayer = feedItems[indexPath.row] as! UsersModel
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "PlayersDetails") as! PlayersDetails
nextViewController.name = selectedPlayer.name ?? "nil"
print("Print When select cell (selectedPlayer.name ?? "nil")")
}
And her is the code in the second view:
@IBOutlet weak var lbName: UILabel!
@IBOutlet weak var userImage: UIImageView!
var name = ""
var selectedUser : UsersModel?
override func viewDidLoad() {
super.viewDidLoad()
lbName.text = selectedUser?.name
print("Print when second view open via viewdidload (selectedUser?.name ?? "nil")")
// Do any additional setup after loading the view.
}
I tried to print from the second view but it shows nothing. What mistake I did?
This is what i have out when print:
There is something I can not understand when I print, it start with the secondview?
Note: I am not using Navigation Controller.
Thanks
4
Answers
You have to use pushViewController to take the data to next screen. Just add one more line in didSelectRowAt function as below:
This will take your data to next screen.
You can also try to execute "nextViewController.loadViewIfNeeded()"
Because you just created this ViewController, but did not present it, according to your hierarchical relationship, choose to use prensent or push to present it, it will execute "ViewDidload()"
There are two serious issues:
The main issue is that you assign the name to the
name
property but you are assigningselectedUser?.name
to the label in the destination controller which is of coursenil
The second issue has already been fixed by Taimoor: You have to present or push the controller.
So replace
didSelect
withAnd please never use
NSArray
in Swift. Declare the data source arrayand delete all unnecessary type casts.
you’ve given feedItems’s array type NSArray() instead to
or you assign name to selectedUser?.name of next page so that assign nil value
or that probably wrong
you have to implement at
didSelectRowAt