import UIKit
class ViewController: UIViewController, UITableViewDataSource,UITableViewDelegate,UIPopoverPresentationControllerDelegate{
private var actors = [Actor]()
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Welcome"
tableView.delegate = self
tableView.dataSource = self
downloadJson()
tableView.tableFooterView = UIView()
}
func downloadJson() {
let reponseValue = NetworkOpertions.sharedInstace.getMethod(){ (fetchValue)-> Void in
if fetchValue != nil {
print("MY fetch Value:",fetchValue)
do {
这是如何从模型类获取数据
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch {
print(error)
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return actors.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ActorCell") as? ActorCell else { return UITableViewCell() }
cell.nameLbl.text = actors[indexPath.row].name
cell.DOBLbl.text = actors[indexPath.row].dob
cell.countryCell.text = actors[indexPath.row].country
if let imageURL = URL(string: actors[indexPath.row].image) {
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
cell.imgView.image = image
}
}
}
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
}
import UIKit
class Actors: Codable {
let actors: [Actor]
init(actors: [Actor]) {
self.actors = actors
}
}
class Actor: Codable {
let name: String
let description: String
let dob: String
let country: String
let height: String
let spouse: String
let children: String
let image: String
init(name: String, description: String, dob: String, country: String, height: String, spouse: String, children: String, image: String) {
self.name = name
self.description = description
self.dob = dob
self.country = country
self.height = height
self.spouse = spouse
self.children = children
self.image = image
}
}
//
import UIKit
class NetworkOpertions: NSObject {
// For Singletone class
static let sharedInstace = NetworkOpertions()
private override init() {
}
private var actors = [Actor]()
func getMethod(OnCompletion:@escaping (Bool)-> Void) {
guard let url = URL(string: FETCH_API_URL)else {return}
let session = URLSession.shared.dataTask(with:url){
(data,response,error) in
if let err = error {
print("We have an Api call error:\(err)")
return
}
if let data = data {
print("This is Data:", data)
do{
let decoder = JSONDecoder()
let downloadedActors = try decoder.decode(Actors.self, from: data)
self.actors = downloadedActors.actors
OnCompletion(true)
}
catch let err{
print(err.localizedDescription)
OnCompletion(false)
}
}
}
session.resume()
}
}
我的问题是:在我的networkOpertions类中,我正在更新模型类,并想使用viewController中的值。如果我只是简单地重新加载self.tableView.reloadData(),它什么也没显示。是否还有其他使用方式
答案 0 :(得分:1)
您可以尝试
let reponseValue = NetworkOpertions.sharedInstace.getMethod(){ (fetchValue)-> Void in
if fetchValue {
DispatchQueue.main.async {
self.actors = NetworkOpertions.sharedInstace.actors
self.tableView.reloadData()
}
}