ios - 为什么数组不在 TableView 中?

标签 ios swift

由于某种原因,数组没有被放入 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/

相关文章:

ios - 当每个单元格都有一个 ImageView 覆盖它时,如何让一个 TableView 注册点击?

swift - 无法从 .xib 文件加载 UIView

iphone - 带轨迹的动画

ios - 为什么我不能引用在Swift项目的同一目录中创建的类或结构?

ios - Interface Builder 文件中的未知类 _TtC8<appName>20MasterViewController

ios - Swift 通用函数调用另一个通用函数

iOS:JSONObjectWithData

ios - 在 Swift 中使用 hpple

ios - 滚动WKWebView时播放声音暂停

ios - 调整导航后退按钮 iOS 13