由于某种原因,数组没有被放入 TableView 中,为什么? CellWithIdentifier
已正确实例化,在 Storyboard中单元格具有相同的标识符。还确认阵列已填充。我还已经在 Storyboard上连接了数据源和委托(delegate)。为什么表格 View 不显示数组?
let TextQuery = PFQuery(className: "submit")
TextQuery.findObjectsInBackgroundWithBlock({
(objects: [AnyObject]?, error: NSError?) -> Void in
var object = objects as! [PFObject]
for i in 0...object.count-1{
self.arrayOftexts.append(object[i].valueForKey("text") as! String)
print(self.arrayOftexts)
}
})
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayOftexts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = arrayOftexts[indexPath.row]
print("added to tableview")
return cell
}
最佳答案
你忘记添加self.tableView.reloadData
,-->这是刷新你的数据
for i in 0...object.count-1{
self.arrayOftexts.append(object[i].valueForKey("text") as! String)
print(self.arrayOftexts)
}
// add the following line
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
选择2
for i in 0...object.count-1{
self.arrayOftexts.append(object[i].valueForKey("text") as! String)
print(self.arrayOftexts)
}
// add the following line
if self.arrayOftexts>0
{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
}
else
{
// set to tableview as hidden
}
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
关于ios - 为什么数组不在 TableView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33163776/