gpt4 book ai didi

ios - 为什么即使在获取数据后,cellForRow 函数中的单元格标签和数组仍为空白?

转载 作者:行者123 更新时间:2023-11-29 05:46:56 28 4
gpt4 key购买 nike

我已经使用获取函数将所有内容提取到字典数组中,当我在行的 TableView 单元格中使用它时,它是空白的。

此外,我无法使用数组计数作为行数函数,因为它是空的。我哪里出错了?

整个类代码如下:

var faqs = [[String:String]]()
var questions = NSMutableArray()
var answers = NSMutableArray()

@IBOutlet var faqsTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

fetchFaqs()
self.faqsTableView.reloadData()

}

func fetchFaqs(){

let manager = APIManager()
manager.parsingGet(url: BaseURL.faqs) { (JSON, Status) in
if Status {
let dict = JSON.dictionaryObject
let data = dict!["data"] as! [[String:Any]]
self.faqs = data as! [[String : String]]
DispatchQueue.main.async {
self.faqsTableView.reloadData()
}
}

}

}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questions.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FaqTableCell

for one in self.faqs {
let question = one["question"]
let answer = one["answer"]
self.answers.add(answer!)
self.questions.add(question!)
}
cell.txtAnswer.text = self.answers[indexPath.row] as? String //answer label
cell.txtQuestion.text = self.questions[indexPath.row] as? String //question label
print(self.answers)
return cell

}

最佳答案

您无法在

中准备问题和答案

cellForRowAt indexPath

因为每次重新加载桌面 View 时它都会调用。所以,你可以像下面我改进的那样改进你的代码。

var faqs = [[String:String]]()
var questions = NSMutableArray()
var answers = NSMutableArray()

@IBOutlet var faqsTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
fetchFaqs()
}

func prepareQuesAns() {
self.faqs.forEach { (objFaq) in
let question = objFaq["question"]
let answer = objFaq["answer"]
self.answers.add(answer!)
self.questions.add(question!)
}

DispatchQueue.main.async {
self.faqsTableView.reloadData()
}
}

func fetchFaqs(){

let manager = APIManager()
manager.parsingGet(url: BaseURL.faqs) { (JSON, Status) in
if Status {
let dict = JSON.dictionaryObject
let data = dict!["data"] as! [[String:Any]]
self.faqs = data as! [[String : String]]
prepareQuesAns()
}

}

}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questions.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FaqTableCell
cell.txtAnswer.text = self.answers[indexPath.row] as? String //answer label
cell.txtQuestion.text = self.questions[indexPath.row] as? String //question label
print(self.answers)
return cell

}

希望这个解决方案可以帮助您。

关于ios - 为什么即使在获取数据后,cellForRow 函数中的单元格标签和数组仍为空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56052660/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com