将Firebase数据URL从TableViewCell传递到ViewController

时间:2019-03-03 19:57:20

标签: ios swift xcode firebase

我一直试图将我的Firebase数据从TableViewCell传递到ViewController,却找不到最简单的解决方案。

我有一个带有ReleaseTableViewCells的TableView。这是用Firebase数据填充的。选择ReleaseTableViewCell时,我要将URL数据传递给WebviewsViewController。

Firebase data view

ViewController.swift

@IBOutlet weak var tableView: UITableView!

var refReleases: DatabaseReference!
var releasesData = [ReleaseModel]()

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "ReleaseCell", for: indexPath) as! ReleaseTableViewCell
    var release: ReleaseModel
    release = releasesData[indexPath.row]

    cell.releaseType.text = release.releasetype
    cell.releaseName.text = release.releasename
    cell.releaseDate.text = release.releasedate
    cell.releaseImage.sd_setImage(with: URL(string: release.releaseimage!), placeholderImage: UIImage(named: "placeholder1"))

    return cell
}

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

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let release = releasesData[indexPath.row]
    performSegue(withIdentifier: "ReleasesToWebviews", sender: release)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ReleasesToWebviews" {
        let destVC = segue.destination as! WebviewsViewController
        destVC.release = sender as? ReleaseModel
    }
}

WebviewsController.swift

 var release: ReleaseModel?

 var websites:[Website] = [];

 func loadWebsites() -> [Website] {
    //Pass website from nikewebsite in Firebase
    let url1 = URL(string: "https://www.nike.com")
    let request1 = URLRequest(url: url1!)
    //Pass website from finishlinewebsite in Firebase
    let url2 = URL(string: "https://www.finishline.com")
    let request2 = URLRequest(url: url2!)
    //Pass website from eastbaywebsite in Firebase
    let url3 = URL(string: "https://www.eastbay.com")
    let request3 = URLRequest(url: url3!)
    //Pass website from flightclubwebsite in Firebase
    let url4 = URL(string: "https://www.flightclub.com")
    let request4 = URLRequest(url: url4!)
    //Pass website from footactionwebsite in Firebase
    let url5 = URL(string: "https://www.footaction.com")
    let request5 = URLRequest(url: url5!)


    let website1:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
    website1.webView.load(request1)
    website1.webView.navigationDelegate = self

    let website2:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
    website2.webView.load(request2)
    website2.webView.navigationDelegate = self

    let website3:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
    website3.webView.load(request3)
    website3.webView.navigationDelegate = self

    let website4:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
    website4.webView.load(request4)
    website4.webView.navigationDelegate = self

    let website5:Website = Bundle.main.loadNibNamed("Website", owner: self, options: nil)?.first as! Website
    website5.webView.load(request5)
    website5.webView.navigationDelegate = self


    return [website1, website2, website3, website4, website5]
}

ReleaseModel.swift

class ReleaseModel: NSObject{

var releasetype: String?
var releasename: String?
var releaseprice: String?
var releasedate: String?
var releaseimage: String?
var nikewebsite: String?
var finishlinewebsite: String?
var eastbaywebsite : String?
var footactionwebsite : String?
var flightclubwebsite : String?


init(releasetype:String?, releasename:String?, releaseprice:String?, releasedate:String?, releaseimage:String?, nikewebsite: String?, finishlinewebsite: String?, eastbaywebsite : String?, footactionwebsite : String?, flightclubwebsite : String?) {
    self.releasetype = releasetype
    self.releasename = releasename
    self.releaseprice = releaseprice
    self.releasedate = releasedate
    self.releaseimage = releaseimage
    self.nikewebsite = nikewebsite
    self.finishlinewebsite = finishlinewebsite
    self.eastbaywebsite = eastbaywebsite
    self.footactionwebsite = footactionwebsite
    self.flightclubwebsite = flightclubwebsite
}
}

0 个答案:

没有答案