我正在尝试解析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()
}
}
}
}
}
答案 0 :(得分:0)
我想问题是你在调用gettingNewPost函数时。在didLoad之后立即调用reloadData()函数,当你调用函数来填充表时,你可能甚至都没有得到数据。您是否尝试登录cellForRow函数以查看是否尝试访问nil数组?
答案 1 :(得分:0)
在重新加载数据之前,您应该测试您的tableview是否已初始化:
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()
}
}
}
}
}
}
答案 2 :(得分:0)
我终于找到了这个问题,其中一个守卫是返回零,所以它停止执行其他人,所以我从使用后卫改为下面
let title = data["title"] as? String ?? ""
let href = data["href"] as? String ?? ""
let image = data["image"] as? String ?? ""
let content = data["content"] as? String ?? ""
let type = data["type"] as? String ?? ""