简体   繁体   中英

reloadData doesn't work after parsing with Swift

Though I assigned value(self) to delegate/dataSource, reloadData doesn't work.
So, I set breakpoint, its dataSource hasn't the data.
How do I work reloadData? Here is my example code.

func getData(query: String) {
    // ...(data processing part)
    var parser: NSXMLParser! = NSXMLParser(data: data)
    parser.delegate = self
    parser.parse() // parsing works well.
    self.testTableView.hidden = false // breakpoint 1
    self.testTableView.dataSource = self
    self.testTableView.delegate = self
    self.testTableView.reloadData() 
    // breakpoint 2
    insertTableWithConstraint() // set constraints
    // init. dataSource
    self.testTableView.dataSource = nil
}

I tried both "self.testTableView.~" and "testTableView.~", but It didn't work.
Here is variable sets in my breakpoint

testTableView = (UITableView) 00000001360dee00
some = (UITableView) 0x00000001360dee00
...
_dataSource = (sunny.TestViewController *)0x135d34950
[0] (sunny.TestViewController)
...
testDatas = (NSMutableArray) "4 values"

I append the my CellForRowAtIndex code.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
    cell.textLabel.text = testDatas.objectAtIndex(indexPath.row).valueForKey("title") as NSString
    cell.detailTextLabel?.text = testData.objectAtIndex(indexPath.row).valueForKey("roadAddress") as NSString
    NSLog("cell.textLabel.text : \(cell.textLabel.text)") // It was not shown in exec.
    NSLog("cell.detailTextLabel.text : \(cell.detailTextLabel?.text)") // It was not shown in exec.
    return cell as UITableViewCell
}

I forgot to append the numberOfRowsInSection code. So I append it.

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

1) You shouldn't call self.testTableView.dataSource = nil . If your app crashes without it, it probably means you're trying to access your data without first checking it is not empty.

2) I assume that your NSXMLParser is used to populate your UITableView rows. NSXMLParser works with a delegate mechanism, so you should call testTableView.reloadData() only when you're done parsing, not immediately like you're doing right now. A good place to call it would be in the parserDidEndDocument delegate function.

3) In your func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell function, check that indexPath corresponds to something accessible in your testDatas variable (which I assume is built during XML parsing). This might be why your app is crashing right now.

4) Also, be careful to appropriately set the cell count in the tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int delegate function (probably using testDatas.count ).

First of all don't set, tableview's dataSource & delegate to nil , set them to self .

As you said your numberOfRowsInSection returns exact count of the datasource object. Then try this:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
    cell.textLabel.text = testDatas.objectAtIndex(indexPath.row).valueForKey("title") as NSString
    return cell
}

Hope this helps.. :)

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