A. K. M. Tariqul Islam
A. K. M. Tariqul Islam

Reputation: 2834

Simple UITableView exception

Working with my HelloWorld iOS app. I'm following this tutorial to make an UITableView. When I run my app the lunch screen goes well, then the app freezes and throws an exception, here is it:

-[UITableViewCellContentView setText:]: unrecognized selector sent to instance

My ScientistTableViewController.swift is here

import UIKit

class ScientistTableViewController: UITableViewController {

var scientists = [Scientist]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

    loadScientists()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return scientists.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIndetifier      = "ScientistTableViewCell"
    let cell                = tableView.dequeueReusableCellWithIdentifier(cellIndetifier, forIndexPath: indexPath) as! ScientistTableViewCell

    // Configure the cell...
    let scientist           = scientists[indexPath.row]

    cell.nameShort.text     = scientist.name
    cell.nameFull.text      = scientist.fullName
    cell.lifeTime.text      = scientist.lifespan
    cell.workingArea.text   = scientist.workFields
    cell.imageSmall.image   = scientist.image

    return cell
}


func loadScientists(){
    let img1         = UIImage(named: "AlKhwarizmi")!
    let img2         = UIImage(named: "AlHazen")!
    let img3         = UIImage(named: "OmarKhayyam")!

    //name: String, lifespan: String, fullName: String, workFields: String, image: UIImage
    let scientist1   = Scientist(name: "AlHazen", lifespan: "Lifespan: 780 – 850, Persian (modern Iran)", fullName: "Muḥammad Ibn Musa Al Khwarizmi", workFields: "Mathematics, Astronomy & Geography", image: img1)
    let scientist2   = Scientist(name: "AlHazen", lifespan: "Lifespan: 780 – 850, Persian (modern Iran)", fullName: "Muḥammad Ibn Musa Al Khwarizmi", workFields: "Mathematics, Astronomy & Geography", image: img2)
    let scientist3   = Scientist(name: "AlHazen", lifespan: "Lifespan: 780 – 850, Persian (modern Iran)", fullName: "Muḥammad Ibn Musa Al Khwarizmi", workFields: "Mathematics, Astronomy & Geography", image: img3)


    scientists += [scientist1, scientist2, scientist3]
}
}

My ScientistsTableViewCell.swift is:

import UIKit

class ScientistTableViewCell: UITableViewCell {

@IBOutlet weak var nameShort: UILabel!
@IBOutlet weak var nameFull: UILabel!
@IBOutlet weak var lifeTime: UILabel!
@IBOutlet weak var workingArea: UILabel!
@IBOutlet weak var imageSmall: UIImageView!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

I googled and found many solutions but none of them is clear/working for me. Please tell me where is the problem in my code.

Upvotes: 1

Views: 73

Answers (1)

joern
joern

Reputation: 27620

When connecting your outlets you accidentally connected one of your UILabel outlets in your code to the contentView of your cell in the nib.

Then when trying to set the label's text, the error occurred because the app tried to call setText: on the content view, which does not know that method.

To fix this go through your connections between your .swift file and your nib and make sure that each UILabel outlet is correctly connected to the corresponding UILabel in your nib.

Upvotes: 1

Related Questions