UICollectionView显示在模拟器上,而不显示在实际设备上

时间:2019-03-22 03:47:30

标签: ios swift uicollectionview ios-simulator uicollectionviewcell

在这里构建我的第一个iOS应用程序,我被困住了。我在网上搜索,但没有发现有关此特定问题的任何信息。

基本上,我有一个UICollectionView显示一些数据。一切在模拟器上都可以正常工作,但是在实际的iPhone上,就像UICollectionView不在那样。

没有错误消息,并且该应用程序没有崩溃,并且该应用程序的所有其他元素都正常运行,所以我很困惑。

代码:

//
//  SecondViewController.swift
//
//  Created by pc on 3/5/19.
//  Copyright © 2019 BF. All rights reserved.
//

import UIKit
import WebKit
import SwiftSoup

class SecondViewController: UIViewController, WKNavigationDelegate, UICollectionViewDataSource {


    @IBOutlet var dataTable: UICollectionView!
    @IBOutlet weak var viewHeader: UILabel!
    @IBOutlet weak var viewFooter: UILabel!

    @IBAction func backButtonClicked(_ sender: UIButton) {
        dismissVC()
    }


    var webView: WKWebView!
    var tableContent = [[String]]()
    var vSpinner : UIView?
    var testString = [[String]]()
    var submittedValue2: String = ""
    var currentSelection2: String = ""


    override func viewDidAppear(_ animated: Bool) {
        let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)

        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.style = UIActivityIndicatorView.Style.gray
        loadingIndicator.startAnimating();

        alert.view.addSubview(loadingIndicator)
        present(alert, animated: true, completion: nil)

    }

    override func viewDidLoad() {
        super.viewDidLoad()


        self.dataTable.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        //dataTable.backgroundColor = UIColor.white
        dataTable.delegate = self as? UICollectionViewDelegate
        dataTable.dataSource = self

        if let layout = dataTable.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
        }



        webView = WKWebView()
        webView.navigationDelegate = self
        //view = webView



        let url = URL(string: "https://someWebsite.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true



        runData()

    }





    ///////ADDS DELAY
    func runData() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 4) { // Change `2.0` to the desired number of seconds.

            self.setWebViewValue(name: "txtValue", data: self.submittedValue2, vin: self.currentSelection2)

        }
    }






    ///////// PULLS DATA
    func setWebViewValue(name: String, data: String, vin: String) {


        if vin == "VIN:" {

            webView.evaluateJavaScript("document.getElementById('rbRequest_1').click()", completionHandler: nil)
        }
        else {
        }

        webView.evaluateJavaScript("document.getElementById(\"\(name)\").value = \"\(data)\"", completionHandler: nil)

        webView.evaluateJavaScript("document.getElementById('btnSubmit').click()", completionHandler: nil)

        DispatchQueue.main.asyncAfter(deadline: .now() + 1) { // Change `2.0` to the desired number of seconds.

            self.webView.evaluateJavaScript("document.documentElement.outerHTML.toString()") { (result, error) -> Void in
            if error != nil {
                print(error!)
            }
                let document = try! SwiftSoup.parse(result as! String)

                for row in try! document.select("table[id=\"gvTests\"] tr") {
                    var rowContent = [String]()

                    for col in try! row.select("td") {
                        let colContent = try! col.text()
                        rowContent.append(colContent)
                    }
                    self.tableContent.append(rowContent)
                }
                if self.tableContent.isEmpty == false {
                    //self.tableContent.remove(at: 0)
                    self.tableContent[0] = ["Make","Model","Year","Date","Pass/Fail","Certificate","Referee"]
                }
                print(self.tableContent)
                self.tableContent = self.transpose(input: self.tableContent)
                if self.tableContent.isEmpty == false {
                    self.dataTable.reloadData()
                }
            }
            self.dismiss(animated: false, completion: nil)
        }
    }



    ////// Collection View functions
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if tableContent.isEmpty == false {
            return tableContent[0].count
        }
        else {
            return 0
        }
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        if tableContent.isEmpty == false {
            return tableContent.count
        }
        else {
            return 0
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)


        let title = UILabel(frame: CGRect(x: 0,y:  0,width: cell.bounds.size.width,height: cell.bounds.size.height))



        for subview in cell.contentView.subviews {

            subview.removeFromSuperview()

        }

        cell.contentView.addSubview(title)


        title.text = tableContent[indexPath.section][indexPath.item]
        //title.textColor = UIColor.black

        title.layer.borderWidth = 1
        title.layer.borderColor = UIColor.black.cgColor
        title.textAlignment = NSTextAlignment.center


        return cell
    }


    public func transpose<T>(input: [[T]]) -> [[T]] {
        if input.isEmpty { return [[T]]() }
        let count = input[0].count
        var out = [[T]](repeating: [T](), count: count)
        for outer in input {
            for (index, inner) in outer.enumerated() {
                out[index].append(inner)
            }
        }

        return out
    }



    func dismissVC() {
        dismiss(animated: true, completion: nil)
    }



}

2 个答案:

答案 0 :(得分:0)

这可能是由于collectionView的约束所致。

答案 1 :(得分:0)

结果发现问题不在于UIcollectionview,而是数据为空,因为它不是像在模拟器中运行时那样从网站中提取的。我将为此发布另一个问题。谢谢大家。