简体   繁体   中英

UITableViewCell doesn't show content in Swift

The code is:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = delegate.managedObjectContext!

    let fetchRequest = NSFetchRequest(entityName: "Inspiration")
    var error: NSError?

    let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as? [NSManagedObject]

    inspirations = fetchedResults!

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
    let content = inspirations[indexPath.row]
    cell.textLabel?.text = content.valueForKey("name") as? String

    return cell
}

fetchedResults has some values in it, it shouldn't have problem, something should be wrong with the implementation of UITableViewCell .

Make sure you have both the delegate and dataSource set for your UITableView.

Try adding this in your viewDidLoad func.

        myTableView.delegate = self
        myTableView.dataSource = self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM