TableView.reloadData() is not working ? (SWIFT)

白昼怎懂夜的黑 提交于 2019-12-02 02:56:24

You need to reload your tableview inside Alamofire block

And so it will look like

  Alamofire.request(.GET, "http://www.wis.com/index.php/capp/chapter_questions_details/\(CourseID)/\(EID)/10")
            .responseJSON { (_, _, data, _) in
                println(data)
                let json = JSON(data!)
                let catCount = json.count
                for index in 0...catCount-1 {
                    let q = json[index]["QUESTION"].string
                    self.QArray.append(q!)
                    println(self.QArray)
                }
        self.progress.dismiss()
        self.Exam.reloadData()
  }

and yes ofcourse, as per given answers and comments,

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.QArray?.count ?? 0 // will return 0 rows if QArray is empty
}

This may help!

Use dispatch queue to call your completion handler in .responseJSON , and from there update your UI .

E.G.

Alamofire.request(.GET, postEndpoint, headers: headers)
        .responseJSON { response in 

    // Do something...

    dispatch_async(dispatch_get_main_queue(), {
        // Update your UI here
        self.tableView.reloadData()
     })
}

Sounds like you need to put the reload into the completion handler so that you have data when you try to update the UI.

To be safe (in case of other calls to reload), you can also guard your return value in numberOfRowsInSection by checking whether there are at least 5 items in QArray.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!