[英]reloadData doesn't work after parsing with Swift
Though I assigned value(self) to delegate/dataSource, reloadData doesn't work. 虽然我为委托/数据源分配了值(自我),但reloadData无法正常工作。
So, I set breakpoint, its dataSource hasn't the data. 因此,我设置了断点,它的dataSource没有数据。
How do I work reloadData? 我如何工作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. 我尝试了“ self.testTableView。〜”和“ testTableView。〜”,但是没有用。
Here is variable sets in my breakpoint 这是我的断点中的变量集
testTableView = (UITableView) 00000001360dee00
testTableView =(UITableView)00000001360dee00
some = (UITableView) 0x00000001360dee00一些=(UITableView)0x00000001360dee00
......
_dataSource = (sunny.TestViewController *)0x135d34950_dataSource =(sunny.TestViewController *)0x135d34950
[0] (sunny.TestViewController)[0](sunny.TestViewController)
......
testDatas = (NSMutableArray) "4 values"testDatas =(NSMutableArray)“ 4个值”
I append the my CellForRowAtIndex code. 我附加了我的CellForRowAtIndex代码。
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. 我忘了附加numberOfRowsInSection代码。 So I append it.
所以我附上了。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testDatas.count
}
1) You shouldn't call self.testTableView.dataSource = nil
. 1)您不应调用
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. 2)我假设您的
NSXMLParser
用于填充UITableView
行。 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. NSXMLParser
与委托机制一起使用,因此仅在解析完成后才应调用testTableView.reloadData()
,而不是像现在那样立即调用。 A good place to call it would be in the parserDidEndDocument
delegate function. 调用它的好地方是在
parserDidEndDocument
委托函数中。
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). 3)在您的
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
函数中,检查indexPath
对应于您在testDatas
变量中可访问的内容(我假设它是在XML解析期间构建的)。 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
). 4)另外,请注意在
tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
委托函数(可能使用testDatas.count
)中适当设置单元格计数。
First of all don't set, tableview's dataSource
& delegate
to nil
, set them to self
. 首先不要设置,tableview的
dataSource
& delegate
为nil
,将它们设置为self
。
As you said your numberOfRowsInSection
returns exact count of the datasource object. 正如您所说的,
numberOfRowsInSection
返回数据源对象的确切计数。 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.. :) 希望这可以帮助.. :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.