【问题标题】:prepareForSegue send Pdfs error - could not castprepareForSegue 发送 Pdfs 错误 - 无法转换
【发布时间】:2025-12-12 10:25:02
【问题描述】:

我想问一下如何在 tableView 和 webView 之间正确发送 pdf。 为此,我有 2 个字符串,其中包含 tableView 中的行名称,另一个字符串包含我要传输的 pdf 名称

我一直按照教程中的相应步骤进行操作,但在 prepareForSegue 方法中出现此错误:

Could not cast value of type 'UITableViewCell' (0x1079f1a18) to 'NSNumber' (0x106ab4b88).

错误在这一行:

var seleccion = sender as! Int

单击tableView 中的一行时出现错误,因此无法显示pdf

我的代码:

class PersonajesHistoricosViewController: UIViewController {


var arregloPdfs = ["jobs1.pdf" , "disney1.pdf","jobs1.pdf"]

var arregloGanadores : [String] = [String]()
var pdfSeleccionado:Int = 0


override func viewDidLoad() {
    super.viewDidLoad()

    arregloGanadores = ["Steve Jobs" , "Walt Disney" , "Arnold Schwarzenegger"]



}


//****************TABLE VIEW*************************
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

    //make sure you use the relevant array sizes
    return arregloGanadores.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
    //Aqui podria hacer referencia a otro array con nombres en especifico para cada uno de los pdfs
    cell.textLabel?.text = self.arregloGanadores[indexPath.row]
    cell.imageView?.image = UIImage(named:"Libro.jpg")

    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    pdfSeleccionado = indexPath.row

    self.performSegueWithIdentifier("haciaVisorPdf", sender: pdfSeleccionado)


}



//Por si quiere cambiar el ancho de cada renglon o celda
//   func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
//
//        return 150
//    }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    // HERE IS THE ERROR
    var seleccion = sender as! Int


    if segue.identifier == "haciaVisorPdf"
    {


        var visorPdf: WebView = segue.destinationViewController as! WebView

        visorPdf.nombreArchivo = self.arregloPdfs[seleccion]


    }    

}
}

import UIKit

class WebView: UIViewController {

@IBOutlet weak var webView: UIWebView!   

var nombreArchivo:String = ""   


override func viewDidLoad() {
    super.viewDidLoad()


    println("\(nombreArchivo)")


    zoom()
    mostrarPdf()

}

func zoom () {

    webView.scalesPageToFit = true


}


func mostrarPdf(){


    //Paso 1 : conseguir direccion en tu bundle de tu archivo pdf
    var direccionPdf = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(nombreArchivo, ofType: "pdf")!)

    println(direccionPdf)

    if let pdfData = NSData(contentsOfURL: direccionPdf!) {

        cargarDatos(pdfData)


    }

    else {

    }

}

func cargarDatos(datosDePdf:NSData) {


    self.webView.loadData(datosDePdf, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: nil)


}
}

【问题讨论】:

  • 为什么不使用全局变量pdfSeleccionado 而不是在prepareForSegue 中传递索引?
  • 感谢兄弟的回答。这是因为我需要更多时间来练习和了解 XCode 的工作原理。但这只是时间问题

标签: ios uitableview swift pdf


【解决方案1】:

根据苹果:

sender: 发起 segue 的对象。您可以使用此参数根据启动 segue 的控件(或其他对象)执行不同的操作。


可以从多个来源触发 Segue,因此请使用 segue 和 sender 参数中的信息来消除应用中不同逻辑路径之间的歧义。例如,如果 segue 源自表格视图,则 sender 参数将标识用户单击的单元格。您可以使用该信息在目标视图控制器上设置数据。

您的问题是sender 没有用于您想要的。但是您在 didSelectRowAtIndexPath 中有一个全局变量 pdfSeleccionado 和您的 indexPath.row ,您可以使用它来尝试在 prepareForSegue 中传递它,因为您首先输入 didSelectRowAtIndexPath 然后执行手动继续,像这样:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){   

    if segue.identifier == "haciaVisorPdf" {
       var visorPdf: WebView = segue.destinationViewController as! WebView
       visorPdf.nombreArchivo = self.arregloPdfs[pdfSeleccionado]
    }
}

希望对你有所帮助。

【讨论】:

  • 帮助很大!!谢谢 现在在 webView (class) 函数中发布了一个新错误 mostrarpdf() {
  • 错误看起来像这样:jobs1.pdf 致命错误:在展开可选值时意外发现 nil
  • 好的没问题,如果答案解决了您的问题,请接受。它可能对另一个人有帮助
  • 搞定了兄弟。你能根据新的错误给我一些帮助吗?
  • 方法中打印了什么?