【发布时间】:2019-01-23 11:50:02
【问题描述】:
我有一组对象,我在 tableview 的屏幕上显示。数组中的每个对象都有 URL 属性,可以从中获取附加信息。
在我的例子中,它是一个声明数组,通过向它的 URL 属性发送 GET 请求,我可以获得包含声明发布日期数据的 JSON。
我想在表格视图单元格中表示这个发布日期。我应该在哪里提出请求?
- 在
viewDidLoad在for-in循环中 - 在
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
我尝试了不同的方法,但总是只得到一个响应(没有array.count 那么多的响应)和十几个错误。
请帮助并告诉从哪里开始。
import UIKit
import SafariServices
class DetailDeclarationInfoTableViewController: UITableViewController {
let declarationInfoController = DeclarationInfoController()
var declarationInfo: DeclarationInfo? {
didSet {
print("Received declarationInfo")
}
}
var declarationModifiedDate: [String] = [] {
didSet {
tableView.reloadData()
print("New element in Dates")
print(declarationModifiedDate)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let titleText = String((self.declarationInfo?.items.count)!)
self.navigationItem.title = "Знайдено \(titleText) декларацій"
guard let declarationInfo = declarationInfo else { return }
for declarant in declarationInfo.items {
declarationInfoController.fetchDeclarationDetails(with: declarant.id) { (declarationInfoElement) in
if let declarationInfoElement = declarationInfoElement {
DispatchQueue.main.async {
print(declarationInfoElement)
print("Success!!")
self.declarationModifiedDate.append(declarationInfoElement["declarationType"] as! String)
self.tableView.reloadData()
}
}
}
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let declarationInfo = declarationInfo {
return (declarationInfo.items.count)
} else {
return 0
}
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "declarationInfoCell", for: indexPath)
var personsPlaceOfWork: String = ""
var date: String = ""
if let placeOfWork = declarationInfo?.items[indexPath.row].placeOfWork {
personsPlaceOfWork = placeOfWork
}
if (indexPath.row + 1) <= declarationModifiedDate.count {
date = ", " + declarationModifiedDate[indexPath.row]
}
let cellText = (declarationInfo?.items[indexPath.row].lastname)! + " " + (declarationInfo?.items[indexPath.row].firstname)! + ", " + personsPlaceOfWork + date
cell.textLabel?.text = cellText
cell.textLabel?.numberOfLines = 0
cell.backgroundColor = UIColor.backgroundPink
return cell
}
错误域=NSCocoaErrorDomain 代码=3840 “字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符 0 周围的值无效。} 错误域=NSCocoaErrorDomain Code=3840 “字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符 0 周围的值无效。} 错误域=NSCocoaErrorDomain Code=3840 “字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符 0 周围的值无效。} 错误域=NSCocoaErrorDomain Code=3840 “字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符 0 周围的值无效。} ["declarationYear1": 2016, "declarationType": 1] 成功! Dates ["1"] 中的新元素
【问题讨论】:
-
请显示您目前拥有的代码。在
viewDidLoad中而不是在cellForRow中提出请求 -
你得到什么错误?
-
在控制台的这个例子中,数组中有 5 个元素,但 UI 中只有一个元素有日期
-
用
DispatchGroup获取DeclarationInfoController中的数据,同步API调用并将数组传递给segue中的DetailDeclarationInfoTableViewController。 -
试试
responseString而不是responseJSON
标签: ios json swift uitableview