请您帮我查一下我的代码中的错误?我在我的数据库中有一个图像URL,我正在尝试下载它并在TableView中显示它,但它显示的是nil值。
这是我的班级:
class Post : NSObject {
var Posts : String!
var Image : String!
}
这是我的TableView。
import UIKit
import FirebaseDatabase
import SDWebImage
class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {
@IBOutlet weak var MyImageView: UIImageView!
@IBOutlet weak var tableView: UITableView!
var Ref:FIRDatabaseReference?
var Handle:FIRDatabaseHandle?
var myarray = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
Ref=FIRDatabase.database().reference()
Handle = Ref?.child("Posts").observe(.childAdded ,with: { (snapshot) in
let post = snapshot.value as? Post
self.myarray.append(post!)
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return myarray.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as? TableViewCell {
// cell?.textLabel?.text = PostData[indexPath.row]
let url = URL(string : self.myarray [(indexPath as NSIndexPath).row].Image!)
cell.MyImage.sd_setImage(with: url)
return cell
}else{
let cell = TableViewCell()
let url = URL(string : self.myarray [(indexPath as NSIndexPath).row].Image!)
cell.MyImage.sd_setImage(with: url)
return cell
}
}
}