gpt4 book ai didi

json - UITableView 不使用 SwiftyJSON 和 Alamofire 加载数据

转载 作者:行者123 更新时间:2023-11-30 13:01:25 25 4
gpt4 key购买 nike

我正在尝试解析 json 并将其加载到 CustomTable 中,我的 json 解析成功,但无法在 UI 中加载数据,我尝试添加测试数据,然后显示出来。所以我想这与范围界定有关。我尝试过在同一问题上使用其他答案,但仍然不起作用

import UIKit
import Alamofire
import SwiftyJSON

class NewsViewController: UIViewController,
UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!



var newsPosts = [NewsPost]()

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self

getNewsPost()


}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let post = newsPosts[indexPath.row]

if let cell = tableView.dequeueReusableCell(withIdentifier: "NewsCell") as? NewsPostCell{
cell.configureCell(post: post)
return cell
}else{
let cell = NewsPostCell()
cell.configureCell(post: post)
return cell
}
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsPosts.count
}


func getNewsPost(){
Alamofire.request(URL_BASE + NEWS_URL).responseJSON { response in


let json = JSON(data: response.data!)

if let dict = json.dictionaryObject{
if let result = dict["data"] as? [NSDictionary]{
for data in result{
guard let title = data["title"] as? String else{
return print("title is nil")
}
guard let href = data["href"] as? String else{
return print("href is nil")
}
guard let image = data["image"] as? String else{
return print("image is nil")
}
guard let content = data["content"] as? String else{
return print("content is nil")
}
guard let _ = data["timestamp"] as? String else{
return print("timestamp is nil")
}
guard let type = data["type"] as? String else{
return print("type is nil")
}

print(type)

let post = NewsPost(title: title, href: href,
image: image, timestamp:
"1 day ago", content: content, type: type)

self.newsPosts.append(post)
}
self.tableView.reloadData()
}
}
}
}


}

最佳答案

您应该在重新加载数据之前测试您的表格 View 是否已初始化:

import UIKit
import Alamofire
import SwiftyJSON

class NewsViewController: UIViewController,
UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!



var newsPosts = [NewsPost]()

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self

getNewsPost()


}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let post = newsPosts[indexPath.row]

if let cell = tableView.dequeueReusableCell(withIdentifier: "NewsCell") as? NewsPostCell{
cell.configureCell(post: post)
return cell
}else{
let cell = NewsPostCell()
cell.configureCell(post: post)
return cell
}
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsPosts.count
}


func getNewsPost(){
Alamofire.request(URL_BASE + NEWS_URL).responseJSON { response in


let json = JSON(data: response.data!)

if let dict = json.dictionaryObject{
if let result = dict["data"] as? [NSDictionary]{
for data in result{
guard let title = data["title"] as? String else{
return print("title is nil")
}
guard let href = data["href"] as? String else{
return print("href is nil")
}
guard let image = data["image"] as? String else{
return print("image is nil")
}
guard let content = data["content"] as? String else{
return print("content is nil")
}
guard let _ = data["timestamp"] as? String else{
return print("timestamp is nil")
}
guard let type = data["type"] as? String else{
return print("type is nil")
}

print(type)

let post = NewsPost(title: title, href: href,
image: image, timestamp:
"1 day ago", content: content, type: type)

self.newsPosts.append(post)
}
if self.tableView != nil {
self.tableView.reloadData()
}
}
}
}
}


}

关于json - UITableView 不使用 SwiftyJSON 和 Alamofire 加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39874112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com