Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Junaid Abd
Courses Plus Student 32 PointsSwift error- " 'UITableViewCell' is not a subtype of NSString" & " 'UILabel?' does not have a member named 'text' "
I'm trying to set the text inside a UITableView Cell, and I get these two errors. I'm using the latest version of Xcode.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var TableView1: UITableView!
var items = ["one", "two"]
override func viewDidLoad() {
super.viewDidLoad()
self.TableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.TableView1.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.TableView1.dequeueReusableCellWithIdentifier("cell" as UITableViewCell)
// error- 'UITableViewCell' is not a subtype of NSString
cell.textLabel.text = self.items[indexPath.row]
//error- 'UILabel?' does not have a member named 'text'
return cell;
}
}
2 Answers
need2edit
Courses Plus Student 12,848 PointsYou need to adjust your parenthesis for the first error, otherwise you are casting a string to a cell, when dequeueReusableCellWithIdentifier needs a single parameter as a string, moving that parenthesis would look like this:
var cell:UITableViewCell = self.TableView1.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
For the second error, add a ? to the label as well, as its an optional property of the cell. That looks like this:
cell.textLabel?.text = self.items[indexPath.row]
That should fix it, if you're still having issues please let me know.
need2edit
Courses Plus Student 12,848 PointsOne common option is utilizing tableView delegate methods didSelectRowAtIndexPath: and didDeselectRowAtIndexPath: for each cell if you're looking for "button-like" behavior. For example, tapping on a cell typically highlights it in gray, you perform your action. If you don't also deselect it, then it may stay highlighted depending on what you're doing.
Junaid Abd
Courses Plus Student 32 PointsHow would i then make it switch View Controllers? (I want it so that when a cell is tapped another view controller appears)
Junaid Abd
Courses Plus Student 32 PointsJunaid Abd
Courses Plus Student 32 PointsIt did, thanks a lot! Would you mind telling me how I can use a UITableViewCell as a button?