gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:54:32 26 4
gpt4 key购买 nike

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



}

最佳答案

这可能是由于您的 collectionView 受到限制所致。

关于ios - UICollectionView 显示在模拟器上,但不显示在实际设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55292609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com