【发布时间】:2017-07-28 07:04:12
【问题描述】:
希望有人可以帮助我!现在正在研究这个。基本上我的问题是我不能使用 Alamofire 请求接收到的 UItableviewcontroller 中的数据。我想在 alamofire 请求中放入结构的请求。最后我不能使用放在数组中的信息。看起来数组在函数之外保持为空。为此,我试图关闭,我收到了 viewDidLoad 请求中的表,但仍然无法在该表之外使用它。
我在名为 Section: 的 swift 文件中有一个结构:
struct Mijnproducten {
var productname : String!
var productdesc : String!
var expanded : Bool!
init(productname: String, productdesc: String, expanded: Bool)
{
self.productname = productname
self.productdesc = productdesc
self.expanded = false
}
}
UitableviewController 看起来像这样:
我制作的数组:
var mijnproducten01 = [Mijnproducten]()
下面有alamofire请求的基金:
func GetUserProperty(completion: @escaping ([Mijnproducten]) -> Void) {
Alamofire.request(URL_USER_PROPERTY, method: .post, encoding: JSONEncoding.default) .responseJSON(completionHandler: { response -> Void in
//getting the json value from the server productid, tagid, status, productimage
switch response.result {
case .success(let value):
let jsonArray = JSON(value)
print(value)
var Mijnproducten01 : [Mijnproducten] = []
for (index, dict) in jsonArray {
let thisObject = Mijnproducten(productname: dict["productname"].string!, productdesc: dict["productdesc"].string!, expanded: false )
Mijnproducten01.append(thisObject) }
completion(Mijnproducten01)
case .failure(let error):
print(error)
completion([])
}
self.tableView.reloadData()
})
}
在 viedDidLoad 中:
override func viewDidLoad() {
super.viewDidLoad()
GetUserProperty(completion: { data in
self.mijnproducten01 = data
print("yess", self.mijnproducten01)})
print("nooo",mijnproducten01)
}
yesss 打印有打印信息,nooo 没有。
最终的目标是从数组中减去信息并在tableCell和Header中使用它。
当我把它放在标题函数中时:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "expandableHeaderView") as! ExpandableHeaderView
header.customInit(Productname: mijnproducten01[Mijnproducten].productname, Productdesc: mijnproducten01[Mijnproducten].productdesc, section: section, delegate: self)
//print("print", mijnproducten01)
return header
}
我收到一个错误:mijnproducten01[Mijnproducten].productname。
希望有人能帮上忙!
【问题讨论】:
-
你要扩展tableView吗?
-
是的,这就是我现在想要的。我在 Youtube 上关注了一部电影以获得我想要的东西。但我误读了一些东西。我让一切正常:将 mijnproducten01[Mijnproducten].productname 替换为 mijnproducten01[count].productname,这是一个跟踪数组中行的变量,我是一名学习者,所以我花了一段时间才知道问题出在哪里。谢谢!!
-
你知道如何在 didSelectRowAt 函数的标题字段中检索文本标签吗?我可以检索行文本字段,但我还需要标题/部分文本字段。
-
试试 indexPath.section
-
你不会得到
UILabel,但你可以从你给section的数组中检索该索引中的值。
标签: arrays json swift uitableview