[英]How to not show tableView until all cell data is finished loading in Swift
[英]How to show Api response in tableview cell in swift
我被困在我的代碼中,我正在嘗試向 API 響應 tableview 單元顯示,但我不知道如何在數組中填充數據,所以沒有在我的 tableviewcell 中顯示任何內容。 我正在快速使用客戶單元和 Alamofire。 請改進我的錯誤給我解決方案。
func Api_call()
{
let url = URL(string: "https://dousic.com/api/radiolist")!
let components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
// let fragment = components.fragment!
print(components)
let params = ["user_id":"16" ]
Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default).responseJSON {response in
self.hideActivityIndicator()
var err:Error?
switch response.result {
case .success(let value):
print(value)
let json = JSON(value)
// returns nil if it's not an array
if let resData = json["radioList"].arrayObject
{
self.array_RadioList = resData as! [[String:AnyObject]]
}
if self.array_RadioList.count > 0 {
self.tbl_home.reloadData()
}
case .failure(let error):
err = error
print(err ?? "error .....")
}
}
}`
感謝幫助 。
編輯
Just create a radio list variable like this
var array_RadioList:[JSON]?
Get array from json like this
-
if let resData = json["response"]["radioList"].array {
self.array_RadioList = resData
self.tableView.reloadData()
}
and reload data.And get radio object in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell? = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier)
let radio:JSON? = array_RadioList?[indexPath.row]
cell?.textLabel?.text = radio?["radio_tags"].string
return cell ?? UITableViewCell()
}
如果您調用的 API 制作精良,則應使用 get 方法,而不是 post。 另外,我嘗試使用“ https://dousic.com/api/radiolist?user_id=16 ”但它返回
{
"response": {
"code": "301",
"error": "wrong url"
}
}
這兩件事可能是你的問題,也可能是你的自定義單元格,或者你的 cellforrow 方法......如果你能顯示更多的代碼,它會有所幫助。
編輯
嘗試使用此版本的可選鏈接:
if let resData = json["radioList"].arrayObject as? [[String:AnyObject] {
self.array_RadioList = resData
self.tbl_home.reloadData()
}
並嘗試使用斷點對其進行調試,以查看應用程序是否可以隨心所欲,以及此時您的變量是什么。
如果你從 Api_call() 得到你的 array_RadioList,試試這個
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell : homeCell = tableView.dequeueReusableCell(withIdentifier: "homeCell")! as! homeCell cell.lbl_name?.text = array_RadioList[indexPath.row]["radio_title"] as? String return cell }
並檢查 numberOfRowsInSection 函數。
嘗試這個
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return [self.array_RadioList].count;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.