skip to Main Content

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:

enter image description here
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


  1. You have to use pushViewController to take the data to next screen. Just add one more line in didSelectRowAt function as below:

    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("Selcted (selectedPlayer.name ?? "nil")") 
        self.navigationController?.pushViewController(nextViewController, animated: true)           
    }
    

    This will take your data to next screen.

    Login or Signup to reply.
  2. 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()"

    Login or Signup to reply.
  3. There are two serious issues:

    1. The main issue is that you assign the name to the name property but you are assigning selectedUser?.name to the label in the destination controller which is of course nil

    2. The second issue has already been fixed by Taimoor: You have to present or push the controller.

    So replace didSelect with

    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.selectedUser = selectedPlayer         
        self.present(nextViewController, animated: true)
    }
    

    And please never use NSArray in Swift. Declare the data source array

    var feedItems = [UsersModel]() // why not just `User`?
    

    and delete all unnecessary type casts.

    Login or Signup to reply.
  4. you’ve given feedItems’s array type NSArray() instead to

    var feedItems: UsersModel = [ ]

    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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
           let nextViewController = storyBoard.instantiateViewController(withIdentifier: "PlayersDetails") as! PlayersDetails
           nextViewController.name = feedItems[indexPath.row].name as! UsersModel
           print("Selcted (selectedPlayer.name ?? "nil")") 
           self.navigationController?.pushViewController(nextViewController, animated: true)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search