gpt4 book ai didi

swift - UITableView 不显示原型(prototype)单元格

转载 作者:搜寻专家 更新时间:2023-11-01 07:11:18 26 4
gpt4 key购买 nike

我正在尝试制作一个连接到 chromecast 以在电视上播放视频的应用程序,直到现在我仍在尝试使用两个 View Controller 显示视频链接,一个包含一个让用户获取视频页面的 WebView ,另一种是显示从第一个 View 推断出的所有视频链接,让用户从页面中选择要转换的视频。我能够获取链接,但问题是它不想显示在表格 View 单元格中。我尝试了很多方法,但我注意到,出于某种原因,根本没有调用 UITableViewDataSource 方法。这是代码:

ViewController.swift:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {
//MARK: Outlets
@IBOutlet weak var searchBar: UITextField!
@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var cancelButton: UIButton!
@IBOutlet weak var searchBarTrailingConstraint: NSLayoutConstraint!

//MARK: Properties
static var videoURLs: [String] = []

//MARK: Methods
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
cancelButton.layer.cornerRadius = 5
cancelButton.isHidden = true
webView.delegate = self

}

func webViewDidFinishLoad(_ webView: UIWebView) {
var videoTag = ""
var embedTag = ""
let htmlCode = webView.stringByEvaluatingJavaScript(from: "document.documentElement.outerHTML")
let htmlTags = htmlCode!.components(separatedBy: "\n") as [String]
for tag in htmlTags{
var videoURL = ""
if tag.contains("<video") {
videoTag = tag.substring(from: tag.range(of: "<video")!.lowerBound)
videoTag = videoTag.substring(to: (videoTag.range(of: ">")?.upperBound)!)
if videoTag.contains("src"){
videoTag = tag.substring(from: tag.range(of: "src")!.upperBound)
for x in videoTag.characters{
if x == "\""{
continue
}else if x == "="{
continue
}else if x == ">"{
break
}else{
videoURL.append(x)
}
}
}
ViewController.videoURLs.append(videoURL)
}
if tag.contains("<embed") {
embedTag = tag.substring(from: tag.range(of: "<embed")!.lowerBound)
embedTag = embedTag.substring(to: (embedTag.range(of: ">")?.upperBound)!)
if embedTag.contains("src"){
embedTag = tag.substring(from: tag.range(of: "src")!.upperBound)
for x in embedTag.characters{
if x == "\""{
continue
}else if x == "="{
continue
}else if x == ">"{
break
}else{
videoURL.append(x)
}
}
}
ViewController.videoURLs.append(videoURL)
}
}
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "Done")))
}

//MARK: Actions
@IBAction func cancelPressed() {
cancelButton.isHidden = true
searchBarTrailingConstraint.constant = 0.0
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
searchBar.resignFirstResponder()
}

@IBAction func searchBarPressed() {
searchBarTrailingConstraint.constant = (cancelButton.frame.width + 8.0) * -1
UIView.animate(withDuration: 0.25) {
self.view.layoutIfNeeded()
}
cancelButton.isHidden = false
}

@IBAction func returnButtonPressed(_ sender: UITextField) {
cancelPressed()
if let url = URL(string: sender.text!){
if UIApplication.shared.canOpenURL(url){
let request = URLRequest(url: url)
webView.loadRequest(request)
}else{
let googleSearchURL = URL(string: "https://www.google.com/search?client=safari&q=\(url)&ie=UTF-8&oe=UTF-8")
let request = URLRequest(url: googleSearchURL!)
webView.loadRequest(request)
}
}else{
var searchString: [String] = []
var searchWord = ""
for x in (sender.text?.characters)!{
if x == " "{
searchString.append(searchWord)
searchWord = ""
}else{
searchWord.append(x)
}
}
//For appending the last word not followed by a space
if !(searchString.last == searchWord){
searchString.append(searchWord)
}
var googleSearchURL = "https://www.google.com/search?client=safari&ie=UTF-8&oe=UTF-8&q="
for element in searchString{
googleSearchURL.append(element)
if !(searchString.last == element){
googleSearchURL.append("+")
}

}
let request = URLRequest(url: URL(string:googleSearchURL)!)
webView.loadRequest(request)
}
}

@IBAction func backButtonPressed(_ sender: UIButton) {
if webView.canGoBack{
webView.goBack()
}
}

@IBAction func forwardButtonPressed(_ sender: UIButton) {
if webView.canGoForward {
webView.goForward()
}
}

}

媒体 TableView Controller :

import UIKit
import AVFoundation

class MediaTableViewController: UIViewController, UITableViewDataSource {

var videoURLs: [String] = []
var videoScreenshots: UIImage!

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(MediaTableViewController.replyToNotification), name: nil, object: nil)

self.tableView.dataSource = self

}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("NOW!\n\n\n", indexPath.count)

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MediaTableViewCell

cell.videoImage.image = videoScreenshot(url: videoURLs[indexPath.count])
cell.videoURL.text = videoURLs[indexPath.count]

return cell
}

@objc func replyToNotification(){
videoURLs = ViewController.videoURLs
ViewController.videoURLs = []
}

// MARK: - Table view data source

func videoScreenshot(url: String) -> UIImage? {

let asset = AVURLAsset(url: URL(string: url)!)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true

do {
let imageRef = try generator.copyCGImage(at: CMTime(value: asset.duration.value/2, timescale: asset.duration.timescale), actualTime: nil)
return UIImage(cgImage: imageRef)
}
catch let error as NSError
{
print("Image generation failed with error \(error)")
return nil
}
}

}

MediaTableViewCell.swift:

import UIKit

class MediaTableViewCell: UITableViewCell {

@IBOutlet weak var videoImage: UIImageView!
@IBOutlet weak var videoURL: UITextView!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

}

这是 Main.storyboard:

main.storyboard

最佳答案

您没有调用 reloadData() 因此 tableView 处于空闲状态。

要解决此问题,请将以下内容添加到 MediaTableViewController:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.reloadData()
}

关于swift - UITableView 不显示原型(prototype)单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44738162/

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