Search code examples
iosswiftuitableviewcell

Returning a Prototype Cell with a Label


I am attempting to connect a label to a prototype cell and return it successfully. I am having some trouble doing so. I have connected my MyCustomTableViewCell. There are officially no "errors" in the app that are detected by xCode. However, when I run it, I am simply getting back a blank table view. Further down in the code (second to last line), I put down "print("Hi!")" to check and see if the code was processing, and the "Hi!" does not show up in the console either when run. Here is my current code:

import UIKit

class MyCustomTableViewCell: UITableViewCell {

    @IBOutlet weak var personText: UILabel!

}



class UserFeedTableViewController: UITableViewController, ComposeViewControllerDelegate {

    private var posts: [PFObject]? {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Query for feeds
        Downloader.sharedDownloader.queryForPosts()

        // Add observers
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "queryFeeds:", name: queryNotification, object: nil)
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    // Notification SEL
    func queryFeeds(notification: NSNotification) {
        posts = notification.object as? [PFObject]
    }

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

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "postSegue" {

            let nav = segue.destinationViewController as! UINavigationController
            let composeVc = nav.topViewController as! ComposeViewController
            composeVc.delegate = self

        }
    }
        func dismissComposeViewController(viewController: ComposeViewController) {
            dismissViewControllerAnimated(true, completion: nil)

        }

        func reloadTableViewAfterPosting() {
            dismissViewControllerAnimated(true, completion: nil)
            Downloader.sharedDownloader.queryForPosts()

        }

    }


    // Mark Table View Data Source
    extension UserFeedTableViewController {

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts?.count ?? 0

    }


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

        if let posts = posts {
            let object = posts[indexPath.row]
            cell.personText?.text = object["post"] as? String
            cell.personText?.numberOfLines = 0

        }
        print("Hi!")
        return cell
    }
}

How can I alter it so it returns the cell?


Solution

  • Double check return posts?.count. Try return posts?.count ?? 0 to return 1 to test

    Update: I remove my mistake.

    Hope this can help.