从API调用表视图填充单元格
问题描述:
我不得不使用异步调用(我猜swift中的闭包)来获取我需要使用SDK(APIWrapper)所需的数据。我发现,在我能够获得我需要的数据之前,这个观点正处于初始阶段。从API调用表视图填充单元格
所以我的第一个问题是,我怎么能让我的单元格在视图加载之前将需要的数据带入表视图?那么,为什么我要在这一点上使用asyncronous呼叫
import APIWrapper
import UIKit
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let provider = APIWrapper
var categories = [String]()
//define number of cells
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
categories = []
self.getCells()
print("count " , self.categories.count)
return(self.categories.count)
}
//get number of cells
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "categories")
cell.textLabel?.text = categories[indexPath.row]
return(cell)
}
private func getCells(){
provider?.getCategoriesWithCallback { (response, error) ->() in
if error == nil {
print("response ", response)
self.updateTableViewWithCategories(categories: response as! [APIWrapperCategory])
}
else {
print("FUCKK")
}
}
}
private func updateTableViewWithCategories(categories: [APIWrapperCategory]){
for category in categories{
print("category obj " , category)
print("category name " , category.name)
}
}
}
从我的控制台输出看起来像
count 0
count 0
count 0
count 0
response Optional([<APIWrapperCategory: 0x6000002a0300>])
category obj <ZDKHelpCenterCategory: 0x6000002a0300>
category name General
response Optional([<ZDKHelpCenterCategory: 0x6180002a30c0>])
category obj <ZDKHelpCenterCategory: 0x6180002a30c0>
category name General
response Optional([<ZDKHelpCenterCategory: 0x6180002a30c0>])
category obj <ZDKHelpCenterCategory: 0x6180002a30c0>
category name General
response Optional([<ZDKHelpCenterCategory: 0x6180002a3300>])
category obj <ZDKHelpCenterCategory: 0x6180002a3300>
category name General
答
您是来自的数据源方法的表视图中获取数据实现代码如下。
从API调用获取数据,调用self.getCells()
方法在您的视图控制器的viewDidLoad()
方法是这样的:
override func viewDidLoad() {
//your code here
//get cells data
self.getCells()
}
而且你的API响应数据添加到表视图的数据源为:
private func updateTableViewWithCategories(categories: [APIWrapperCategory]){
self. categories = []
for category in categories{
print("category obj " , category)
print("category name " , category.name)
self. categories.append(category.name)
}
//reload table view here
DispatchQueue.main.async {
self.yourTableView.reloadData()
}
}
,改变委托方法:
//define number of cells
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("count " , self.categories.count)
return(self.categories.count)
}
答
所以我的e nded起来使用viewWillAppear
和数据返回,使细胞填充所以这里的方式改变了一些东西是代码,我希望这能帮助别人走出
@IBOutlet weak var tableView: UITableView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.getCategoriesFromZendesk()
}
//define number of cells
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("count " , self.categories.count)
return self.categories.count
}
//get number of cells
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Add Label to the Prototype Cell
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "categories")
cell.textLabel?.text = categories[indexPath.row]
return(cell)
}
private func getCategories(){
self.categories = []
provider?.getCategoriesWithCallback { (response, error) ->() in
if error == nil {
print("response ", response?.map{($0 as AnyObject as? APIWrapperCategory)?.name ?? ""} ?? "empty")
self.updateTableViewWithCategories(categories: response as? [APIWrapperCategory])
}
else {
print("FUCKK")
}
}
}
private func updateTableViewWithCategories(categories: [APIWrapperCategory]?){
self.categories = categories?.flatMap{$0.name} ?? []
tableView.reloadData()
}
确保调用'正在做reloadData'在主队列上。 – rmaddy
谢谢@rmaddy我已经添加了这个。 –