如何将 API 数据传递给 table 视图单元格
How to pass API data to table view cells
我在将 API 返回的数据传递给 table 视图单元格时遇到了一些问题。我将数据附加到一个数组,然后将该数组传递给 table 视图(像往常一样)以获取单元格的行数和数据。当我在我追加的函数中打印时,标题显示在数组中。在外面他们不是。任何的想法?相关代码如下:
class ProductTableViewController: UITableViewController, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet var tabView: UITableView!
var filteredData = ["Title1"]
override func viewDidLoad() {
super.viewDidLoad()
getProducts { (products) in
for product in products {
self.filteredData.append(product.title)
}
}
}
func getProducts(completionHandler: @escaping([ProductDetail]) -> Void) {
let url = URL(string: "exampleAPIURL")!
let dataTask = URLSession.shared.dataTask(with: url) {data, _, _ in
guard let jsonData = data else { return }
do {
let decoder = JSONDecoder()
let productsResponse = try decoder.decode(Products.self, from: jsonData)
let productDetails = productsResponse.data
for name in productDetails {
self.filteredData.append(name.title)
}
completionHandler(productDetails)
}catch {
print(error.localizedDescription)
}
}
dataTask.resume()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if filteredData == nil {
return 1 }
else {
return filteredData.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
for name in filteredData {
if name != nil {
let product = filteredData[indexPath.row]
cell.textLabel?.text = product
} else {
cell.textLabel?.text = "name"
}
}
return cell
}
当我 运行 模拟器时,我只收到 filteredData
数组中的硬编码字符串。是否有不同的方式来传递 JSON?
非常感谢!
收集数据后重新加载 table 视图:
getProducts { (products) in
for product in products {
self.filteredData.append(product.title)
}
self.tabView.reloadData()
}
设置好数组后,需要调用self.tableView.reloadData()
,在主线程调用
此外,最好从 viewDidAppear
调用产品 API 就像从 viewDidLoad
returns 调用 API 一样快,操作视图可能会失败。此外,您可能还想显示一些 activity 指标。
override func viewDidAppear() {
super.viewDidLoad()
getProducts { (products) in
for product in products {
self.filteredData.append(product.title)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
我在将 API 返回的数据传递给 table 视图单元格时遇到了一些问题。我将数据附加到一个数组,然后将该数组传递给 table 视图(像往常一样)以获取单元格的行数和数据。当我在我追加的函数中打印时,标题显示在数组中。在外面他们不是。任何的想法?相关代码如下:
class ProductTableViewController: UITableViewController, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet var tabView: UITableView!
var filteredData = ["Title1"]
override func viewDidLoad() {
super.viewDidLoad()
getProducts { (products) in
for product in products {
self.filteredData.append(product.title)
}
}
}
func getProducts(completionHandler: @escaping([ProductDetail]) -> Void) {
let url = URL(string: "exampleAPIURL")!
let dataTask = URLSession.shared.dataTask(with: url) {data, _, _ in
guard let jsonData = data else { return }
do {
let decoder = JSONDecoder()
let productsResponse = try decoder.decode(Products.self, from: jsonData)
let productDetails = productsResponse.data
for name in productDetails {
self.filteredData.append(name.title)
}
completionHandler(productDetails)
}catch {
print(error.localizedDescription)
}
}
dataTask.resume()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if filteredData == nil {
return 1 }
else {
return filteredData.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
for name in filteredData {
if name != nil {
let product = filteredData[indexPath.row]
cell.textLabel?.text = product
} else {
cell.textLabel?.text = "name"
}
}
return cell
}
当我 运行 模拟器时,我只收到 filteredData
数组中的硬编码字符串。是否有不同的方式来传递 JSON?
非常感谢!
收集数据后重新加载 table 视图:
getProducts { (products) in
for product in products {
self.filteredData.append(product.title)
}
self.tabView.reloadData()
}
设置好数组后,需要调用self.tableView.reloadData()
,在主线程调用
此外,最好从 viewDidAppear
调用产品 API 就像从 viewDidLoad
returns 调用 API 一样快,操作视图可能会失败。此外,您可能还想显示一些 activity 指标。
override func viewDidAppear() {
super.viewDidLoad()
getProducts { (products) in
for product in products {
self.filteredData.append(product.title)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}