TableView.reloadData()不工作? (迅速)

我正在开发一个测验应用程序,从JSON中提取问题。 我已经多次使用reloadData for TableView&按预期工作。 但现在我正在使用Alamofire将问题保存到一个数组中,并从数组中获取问题。

但它返回数组是不合格的。

我如何显示第一个单元格中的数组的第一项?

虚电路

class ExamController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var Exam: UITableView! var CourseID : String! var EID : String! var EName : String! var ET : String! var QArray : [String] = [] var progress = GradientCircularProgress() override func viewDidLoad() { super.viewDidLoad() progress.show(style:MyStyle()) // JUST TRIED dispatch_async(dispatch_get_main_queue(), { self.getQuestions() // DO SOMETHING ON THE MAINTHREAD self.Exam.reloadData() }) self.Exam.tableFooterView = UIView(frame: CGRectZero) self.Exam.tableFooterView?.hidden = true } func getQuestions(){ if ET == "CHAPTER" { 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() } else if ET == "FA" { Alamofire.request(.GET, "http://www.wis.com/index.php/capp/fa_questions_by_course_id/\(CourseID)/\(EID)") .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() } } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if indexPath.row == 0{ let cell = self.Exam.dequeueReusableCellWithIdentifier("Question") as! QuestionCell cell.Questionlabel?.text = QArray[0] return cell } else { let cell = self.Exam.dequeueReusableCellWithIdentifier("Option") as! OptionCell cell.Optionlabel?.text = "Option" return cell } } 

你需要重新加载你的Alamofire块内的tableview

所以它看起来像

  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() } 

当然,按照给出的答案和评论,

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

这可能有帮助!

使用调度队列调用你的完成处理程序在.responseJSON,并从那里更新您的用户界面。

例如

 Alamofire.request(.GET, postEndpoint, headers: headers) .responseJSON { response in // Do something... dispatch_async(dispatch_get_main_queue(), { // Update your UI here self.tableView.reloadData() }) } 

听起来就像你需要将重新加载到完成处理程序,以便您尝试更新UI时有数据。

为了安全起见(在其他调用重载的情况下),还可以通过检查QArray是否至less有5个项目来保护numberOfRowsInSection的返回值。