CollectionView为Swift 4中的Segue做准备

时间:2018-01-25 17:52:28

标签: swift xcode uicollectionview

我在将数据解析到具有自定义单元格的uicollectionview的详细viewcontroller时出现问题,但我使用相同的数据处理了uitableview。

以下是我的代码的segue部分准备:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){

        if segue.identifier == "movetodetail" {
        if let indexPath = topTableView.indexPathForSelectedRow{
            let selectedRow = indexPath.row
            let detailVC = segue.destination as! DetailedFirstMainViewController
            detailVC.topImage = self.QponsArray[selectedRow].offer_image2_loc
            detailVC.topTitletxt = self.QponsArray[selectedRow].offer_title
            detailVC.topProfileIMG = self.QponsArray[selectedRow].offer_image_loc
            detailVC.topDes = self.QponsArray[selectedRow].offer_desc
            detailVC.offerStart = self.QponsArray[selectedRow].offer_start
            detailVC.offerEnd = self.QponsArray[selectedRow].offer_end
        }

          else  if segue.identifier == "movetodetail1" {

            guard let cell = sender as? TopCollectionViewCell else {
                    return
                }
                guard let indexPath1 = topCollectionView.indexPath(for: cell) else {
                    return
                }

                    let detailVC1 = segue.destination as! DetailedFirstMainViewController
                detailVC1.topImage = QponsArray[indexPath1.row].offer_image2_loc

                detailVC1.topTitletxt = self.QponsArray[indexPath1.row].offer_title
                detailVC1.topProfileIMG = self.QponsArray[indexPath1.row].offer_image_loc
                detailVC1.topDes = self.QponsArray[indexPath1.row].offer_desc
                detailVC1.offerStart = self.QponsArray[indexPath1.row].offer_start
                detailVC1.offerEnd = self.QponsArray[indexPath1.row].offer_end


            }

没有发送任何内容,详细视图对所有属性重新调整为零。我确实质疑这是否是Optionals的一个问题但是我没有得出任何结论或者可以找到任何问题,但我已经碰壁了。

我们非常感谢所需的任何帮助或示例。

以下是此视图的完整代码:

import UIKit
import SwiftyJSON
import SDWebImage
import CenteredCollectionView


struct Qpons {
    let offer_id: Int
    let offer_title: String
    let offer_image_loc: String
    let offer_image2_loc: String
    let offer_start: String
    let offer_end: String
    let company_id: Int
    let address_id: Int
    let user_id: Int
    let offer_type: String
    let offer_desc: String
    let address_line1: String
    let address_line2: String
    let address_town: String
    let address_county: String
    let address_postcode: String
    let address_type: String
    let address_long: Double
    let address_lat: Double
    let address_payments_cash: Int
    let address_payments_cheque: Int
    let address_payments_contactless: Int
    let address_payments_visa: Int
    let address_payments_mastercard: Int
    let address_payments_maestro: Int
    let address_payments_americanexpress: Int
    let address_payments_jcb: Int
    let address_payments_dinersclub: Int
    let company_legal_name: String
    let company_trading_name: String
    let company_type: String
    let company_vat: String
    let company_reg_no: String
    let company_logo_loc: String
    let company_website: String
    let company_facebook: String
    let company_twitter: String
    let company_instagram: String
    var cat_name: String
    let sub_cat_name: String

}

class FirstMainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource {


    @IBOutlet weak var dateLbl: UILabel!

    @IBOutlet weak var topCollectionView: UICollectionView!
    @IBOutlet weak var topTableView: UITableView!
    var QponsArray: [Qpons] = []



    final let url = URL(string: "http://***/test-site/app_calls/getalloffers.php")



    override func viewDidLoad() {
        super.viewDidLoad()
            downloadJson()


        let dateformatter = DateFormatter()

        dateformatter.dateFormat = "EEEE MMMM dd, yyyy"

        let now = dateformatter.string(from: NSDate() as Date)
        dateLbl.text = now



        topTableView.register(UINib(nibName: "TopTableViewCell", bundle: nil), forCellReuseIdentifier: "topCell")

        let nibName = UINib(nibName: "TopCollectionViewCell", bundle:nil)

        topCollectionView.register(nibName, forCellWithReuseIdentifier: "topCCell")

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func downloadJson() {
        guard let downloadURL = url else { return }
        URLSession.shared.dataTask(with: downloadURL) { data, urlResponse, error in
            guard let data = data, error == nil, urlResponse != nil else {
                print("something is wrong")
                return
            }
            print("downloaded")
            do
            {

                let jsonArray = try JSON(data: data)

                for (index, dict) in jsonArray {
                    let thisObject = Qpons(


                        offer_id: dict["offer_id"].intValue,
                        offer_title: dict["offer_title"].stringValue,
                        offer_image_loc: dict["offer_image_loc"].stringValue,
                        offer_image2_loc: dict["offer_image2_loc"].stringValue,
                        offer_start: dict["offer_start"].stringValue,
                        offer_end: dict["offer_end"].stringValue,
                        company_id: dict["company_id"].intValue,
                        address_id: dict["address_id"].intValue,
                        user_id: dict["user_id"].intValue,
                        offer_type: dict["offer_type"].stringValue,
                        offer_desc: dict["offer_desc"].stringValue,
                        address_line1: dict["address_line1"].stringValue,
                        address_line2: dict["address_line2"].stringValue,
                        address_town: dict["address_town"].stringValue,
                        address_county: dict["address_country"].stringValue,
                        address_postcode: dict["address_postcode"].stringValue,
                        address_type: dict["address_type"].stringValue,
                        address_long: dict["address_long"].doubleValue,
                        address_lat: dict["address_lat"].doubleValue,
                        address_payments_cash: dict["address_payments_cash"].intValue,
                        address_payments_cheque: dict["address_payments_cheque"].intValue,
                        address_payments_contactless: dict["address_payments_contactless"].intValue,
                        address_payments_visa: dict["address_payments_visa"].intValue,
                        address_payments_mastercard: dict["address_payments_mastercard"].intValue,
                        address_payments_maestro: dict["address_payments_maestro"].intValue,
                        address_payments_americanexpress: dict["address_payments_americanexpress"].intValue,
                        address_payments_jcb: dict["address_payments_jcb"].intValue,
                        address_payments_dinersclub: dict["address_payments_dinersclub"].intValue,
                        company_legal_name: dict["company_legal_name"].stringValue,
                        company_trading_name: dict["company_trading_name"].stringValue,
                        company_type: dict["company_type"].stringValue,
                        company_vat: dict["company_vat"].stringValue,
                        company_reg_no: dict["company_reg_no"].stringValue,
                        company_logo_loc: dict["company_logo_loc"].stringValue,
                        company_website: dict["company_website"].stringValue,
                        company_facebook: dict["company_facebook"].stringValue,
                        company_twitter: dict["company_twitter"].stringValue,
                        company_instagram: dict["company_instgram"].stringValue,
                        cat_name: dict["cat_name"].stringValue,
                        sub_cat_name: dict["sub_cat_name"].stringValue





                    )
                    self.QponsArray.append(thisObject)
                }

//                let offers = json["offer_title"].string
//                 print(offers)


                print(self.QponsArray)



                DispatchQueue.main.async {
                    self.topTableView.reloadData()
                    self.topCollectionView.reloadData()
                }
            } catch {
                print("something wrong after downloaded")
            }
            }.resume()
    }








    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->
        UICollectionViewCell {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topCCell", for: indexPath) as! TopCollectionViewCell

            let qponImage = QponsArray[indexPath.row].offer_image_loc




            cell.qponImage.sd_setImage(with: URL(string: "http://****/test-site/images/qpon_logos/\(qponImage)"), placeholderImage: UIImage(named: "placeholder.png"))



            return (cell)

    }



    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.QponsArray.count
    }




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

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "topCell", for: indexPath) as! TopTableViewCell



        let qponImage = QponsArray[indexPath.row].offer_image_loc

        cell.qponImage.sd_setImage(with: URL(string: "http://****/test-site/images/qpon_logos/\(qponImage)"), placeholderImage: UIImage(named: "placeholder.png"))

        cell.qponTitle.text = QponsArray[indexPath.row].offer_title
        cell.qponDes.text = QponsArray[indexPath.row].offer_desc

        cell.selectionStyle = .none


        return (cell)
    }



    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "movetodetail", sender: indexPath)
    }

    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "movetodetail1", sender: indexPath)
    }



    override func prepare(for segue: UIStoryboardSegue, sender: Any?){

        if segue.identifier == "movetodetail" {
        if let indexPath = topTableView.indexPathForSelectedRow{
            let selectedRow = indexPath.row
            let detailVC = segue.destination as! DetailedFirstMainViewController
            detailVC.topImage = self.QponsArray[selectedRow].offer_image2_loc
            detailVC.topTitletxt = self.QponsArray[selectedRow].offer_title
            detailVC.topProfileIMG = self.QponsArray[selectedRow].offer_image_loc
            detailVC.topDes = self.QponsArray[selectedRow].offer_desc
            detailVC.offerStart = self.QponsArray[selectedRow].offer_start
            detailVC.offerEnd = self.QponsArray[selectedRow].offer_end
        }

          else  if segue.identifier == "movetodetail1" {

            guard let cell = sender as? TopCollectionViewCell else {
                    return
                }
                guard let indexPath1 = topCollectionView.indexPath(for: cell) else {
                    return
                }

                    let detailVC1 = segue.destination as! DetailedFirstMainViewController
                detailVC1.topImage = QponsArray[indexPath1.row].offer_image2_loc

                detailVC1.topTitletxt = self.QponsArray[indexPath1.row].offer_title
                detailVC1.topProfileIMG = self.QponsArray[indexPath1.row].offer_image_loc
                detailVC1.topDes = self.QponsArray[indexPath1.row].offer_desc
                detailVC1.offerStart = self.QponsArray[indexPath1.row].offer_start
                detailVC1.offerEnd = self.QponsArray[indexPath1.row].offer_end


            }

        }


    }


}

添加数据的详细视图如下:

class DetailedFirstMainViewController: UIViewController {
    @IBOutlet weak var topCover: UIImageView!
    @IBOutlet weak var topTitle: UILabel!
    @IBOutlet weak var topProfile: UIImageView!
    @IBOutlet weak var textDes: UILabel!
    @IBOutlet weak var offerstartLB: UILabel!
    @IBOutlet weak var offerendLB: UILabel!

    @IBOutlet weak var line2: UIView!
    @IBOutlet weak var line1: UIView!

    var topImage: String = ""
    var topTitletxt: String = ""
    var topProfileIMG: String = ""
    var topDes: String = ""
    var offerStart: String = ""
    var offerEnd: String = ""

0 个答案:

没有答案