How much does nil return? I just learn from this, do not judge strictly.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "Message" let cellMessage = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MessageTableViewCell // Configure the cell... cellMessage.useravaImageView.image = UIImage(named: messageAvatarUser[indexPath.row]) cellMessage.usermessageTextLabel.text = messageText[indexPath.row] cellMessage.timemessageTextLabel.text = timeMessageFrom[indexPath.row] cellMessage.usernameTextLabel.text = userNameFrom[indexPath.row] cellMessage.idTextLabelUser.text = idUserFrom[indexPath.row] return cellMessage } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let selectedCellMsg = tableView.cellForRowAtIndexPath(indexPath) let selectMSG = selectedCellMsg!.textLabel!.text print(selectMSG) } 

Data is output, but when an item is selected, it returns nil as if I did not.

    1 answer 1

    Because for textLabel you have not assigned anything. For example, get usermessageTextLabel directly from UITableView :

     let selectedCellMsg = tableView.cellForRowAtIndexPath(indexPath)! as! MessageTableViewCell let selectMSG = selectedCellMsg.usermessageTextLabel!.text 

    And even easier from the source:

     let selectMSG = messageText[indexPath.row]