TableView.reloadData()不工作? (迅速) [英] TableView.reloadData() is not working ? (SWIFT)
问题描述
我开发一个测验应用,该应用获取从JSON问题。我已经使用reloadData为TableView中多次和放大器;和预期一样。但现在我使用读取&Alamofire问题放大器;保存在一个阵列和放大器;从阵列获取的问题。
但它返回数组是出指数。
我怎样才能显示阵列的第一项中的第一个单元格?
VC
类ExamController:UIViewController的,UITableViewDataSource,{的UITableViewDelegate @IBOutlet弱VAR考试:UITableView的! VAR CourseID:串!
VAR EID:串!
VAR的ename:串!
VAR ET:串! VAR QArray:[字符串] = [] VAR进度= GradientCircularProgress()
覆盖FUNC viewDidLoad中(){
super.viewDidLoad()
progress.show(风格:myStyle的())
//只是试图
dispatch_async(dispatch_get_main_queue(),{
self.getQuestions()
//做点什么ON THE MAINTHREAD
self.Exam.reloadData()
}) self.Exam.tableFooterView =的UIView(帧:CGRectZero)
self.Exam.tableFooterView?.hidden =真
} FUNC getQuestions(){ 如果ET ==章节{
Alamofire.request(不用彷徨,http://www.wis.com/index.php/capp/chapter_questions_details/\\(CourseID)/\\(EID)/10)
.responseJSON {(_,_,数据,_)中
的println(数据)
让JSON = JSON(数据!)
让catCount = json.count
在0指数... catCount-1 {
让Q = json的[指数] [问题。字符串
self.QArray.append(Q!)
的println(self.QArray)
} } self.progress.dismiss()
self.Exam.reloadData() }否则,如果ET ==FA{
Alamofire.request(。获得http://www.wis.com/index.php/capp/fa_questions_by_course_id/\\(CourseID)/\\(EID))
.responseJSON {(_,_,数据,_)中
的println(数据)
让JSON = JSON(数据!)
让catCount = json.count
在0指数... catCount-1 {
让Q = json的[指数] [问题。字符串
self.QArray.append(Q!)
的println(self.QArray)
} }
self.progress.dismiss()
self.Exam.reloadData()
}
} FUNC numberOfSectionsInTableView(的tableView:UITableView的) - GT; INT {
返回1
} FUNC的tableView(的tableView:UITableView的,numberOfRowsInSection段中:int) - GT; INT {
返回5
}
FUNC的tableView(的tableView:UITableView的,的cellForRowAtIndexPath indexPath:NSIndexPath) - GT; {的UITableViewCell
如果indexPath.row == 0 {
让细胞= self.Exam.dequeueReusableCellWithIdentifier(问题)作为! QuestionCell cell.Questionlabel?的.text = QArray [0]
回报细胞 }
其他
{
让细胞= self.Exam.dequeueReusableCellWithIdentifier(选项)作为! OptionCell cell.Optionlabel?的.text =选项
回报细胞 }
}
您需要重新加载 Alamofire
块中您的tableview
所以它看起来像
Alamofire.request(不用彷徨,http://www.wis.com/index.php/capp/chapter_questions_details/\\(CourseID)/\\(EID)/10 )
.responseJSON {(_,_,数据,_)中
的println(数据)
让JSON = JSON(数据!)
让catCount = json.count
在0指数... catCount-1 {
让Q = json的[指数] [问题。字符串
self.QArray.append(Q!)
的println(self.QArray)
}
self.progress.dismiss()
self.Exam.reloadData()
}
和是ofcourse,按给定的答案和注释,
FUNC的tableView(的tableView:UITableView的,numberOfRowsInSection段中:int) - GT; INT {
返回self.QArray?.Count之间? 0 //将返回0行,如果QArray是空的
}
这可以帮助!
I am developing a Quiz App which fetches questions from a JSON. I have already used reloadData for TableView many times & worked as expected. But now I am fetching Questions using Alamofire & saving it in a Array & fetching the Questions from the Array.
But it returns Array is Out of Index.
How can i Display the First item of the Array in the first Cell ?
VC
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
}
}
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!
这篇关于TableView.reloadData()不工作? (迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!