简体   繁体   中英

Not populating UiTableView with Parse.com results in SWIFT

I am hoping someone can help, as I am trying to debug, but am going round in circles. I have a table in Parse.com and can query and retrieve data successfully. I did a test with a println and the correct values of the strings are displayed in the output. What I was trying to do was put these values into a UITableView, but this has taken me down some pretty frustrating paths (I am still trying to learn this as best as I can and sometimes some concepts are hard to comprehend).

My last attempt (see code below) I thought by writing the values to a struct I could use this as I have done in the past, given that I can see the values I need to populate. I don't think this is the right way but I thought it should work.

My code when I put a breakpoint in doesn't get to even defining the tableview :( I know I am missing something but maybe just need a fresh pair of eyes to help me see what I am missing. Any help would be greatly appreciated:

@IBOutlet weak var navlabel: UILabel!

var TopicPassed:String!
var storedsentences=[getsentences]()

@IBOutlet weak var sentencetableview: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    navlabel.text = TopicPassed

    var query = PFQuery(className:"TalkToMeSentences")
    query.whereKey("Topic", equalTo:TopicPassed)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {
            // query successful - display number of rows found
            println("Successfully retrieved \(objects.count) sentences")

            // print sentences found
            for object in objects {

                let retrievedsentences = object["Sentence"] as NSString

                self.storedsentences = [getsentences(parsesentence: "\(retrievedsentences)")]
                println("\(retrievedsentences) ")

            }
            self.sentencetableview.reloadData()
        } else {
            // Log details of the failure
            println("Error: \(error) \(error.userInfo!)")
        }
    }

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return storedsentences.count
}

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

    var sentence : getsentences
    // Configure the cell...
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    cell.textLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping
    sentence = storedsentences[indexPath.row]
    cell.textLabel!.text = sentence.parsesentence
    cell.textLabel?.numberOfLines = 0
    return cell
}

Resolved it, I think.

My problem was I had not assigned outputs for the the datasource or the delegates. Once I did I could get the table to populate.

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