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

问题描述 投票:0回答:2

在这里建立我的第一个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)
    }



}
ios swift uicollectionview ios-simulator uicollectionviewcell
2个回答
0
投票

这可能会导致对collectionView的约束。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.