David Seek David Seek - 4 months ago 13
iOS Question

Swift / How to call tableViewCell's textLabel.text value?

I'm having a subclass of UITableViewCell with an Outlet.

class UserNewPostCell: UITableViewCell {
@IBOutlet weak var textView: UITextView!


The tableView ist part of a UIViewController

class NewUserTweetVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!


My NewUserTweetVC has a Top-Toolbar with a Button

@IBAction func toolbarPostButton(sender: AnyObject, date: NSDate) {

let indexPath = NSIndexPath(forRow: 0, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! UserNewPostCell

let testTweet = cell.textLabel!.text

postTweet(testTweet!, date: NSDate())

}


Problem:
cell.textLabel!.text
= nil. Why? The Placeholder Text is "What's on your mind?" and to make sure I added:

override func awakeFromNib() {
super.awakeFromNib()
textView.text = "What's on your mind?"


To the Cell. How do I get the value of textLabel.text the way I need it too? Help is very appreciated.

Answer

You are setting your textView's text (from @IBOutlet weak var textView: UITextView!) here: textView.text = "What's on your mind?". This is not the same as the cell.textLabel!.text.

The cell.textLabel is an auto-generated parameter for UITableViewCell used in the standard UITableViewCellStyles.

You need either stick to using your textView everywhere or the textLabel.

Comments