【发布时间】:2014-10-26 13:25:31
【问题描述】:
我的应用中有一个表格视图,它必须显示聊天消息列表。我将聊天消息加载到 NSMutableArray 中,在将数据加载到数组后,我需要更新 tableview。
这是我正在使用的代码:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
("reloading data")
self.tableView.reloadData()
})
但它会打印出reloading data,但不会更新/重新加载表格视图。
有什么想法吗?
更新
我意识到它没有调用这个方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println("running")
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
println(listDataArray[indexPath.row])
cell.textLabel?.text = (listDataArray[indexPath.row] as NSString)
return cell
}
但如果我尝试删除它或将其注释掉,他们会说该类不符合 UITableViewDataSource 的协议。
那么它怎么能不调用该方法呢? (NOTE: it is not printing "running" either)
更新 2
这里是 tableview 方法的代码(注意,我在上面发布的时候稍微改变了那个)
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
if(listDataArray.count > 0) {
return listDataArray.count
} else {
return 0
}
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !(cell != nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell?.textLabel?.text = listDataArray[indexPath.row] as? String
return cell
}
【问题讨论】:
-
也许你的数组没有像你想象的那样被更新,或者你的 cellForRowAtIndexPath 是错误的......这可能是很多事情......
-
我知道数组正在更新,因为如果我打印出来,新内容就在那里
-
[self.tableView reloadData];
-
@adnan 不,这是 Swift。
-
@JoshHarington 你能发布你的 cellForRowAtIndexPath 方法吗?你在重复使用细胞吗?
标签: ios uitableview swift