2015-06-01 25 views
1

我想問如何在tableView和webView之間正確發送pdf。 爲此,我有兩個字符串與tableView中的行名稱和另一個與我想要轉讓的pdfs的名稱prepareForSegue發送PDF錯誤 - 無法投射

我一直在遵循教程中的相應步驟,但我得到此錯誤在prepareForSegue方法:

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

錯誤是在這一行:當我在tableView單擊某行

var seleccion = sender as! Int 

錯誤就要到了,所以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) 


} 
} 
+0

爲什麼不使用全局變量'pdfSeleccionado'而不是在'prepareForSegue'中傳遞索引? –

+0

感謝您的兄弟。這是因爲我需要更多時間來練習和理解XCode是如何工作的。但這只是時間問題 – FactorJose

回答

0

據蘋果公司稱:

sender: The object that initiated the segue. You might use this parameter to perform different actions based on which control (or other object) initiated the segue.


Segues can be triggered from multiple sources, so use the information in the segue and sender parameters to disambiguate between different logical paths in your app. For example, if the segue originated from a table view, the sender parameter would identify the cell that the user clicked. You could use that information to set the data on the destination view controller.

你的問題是,sender不用於你想。但是,你必須在didSelectRowAtIndexPath一個全局變量pdfSeleccionadoindexPath.row,你可以用它代替嘗試它通過在prepareForSegue,因爲你先在didSelectRowAtIndexPath輸入,然後手動執行SEGUE,像這樣:

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

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

我希望這對你有所幫助。

+0

它有很多幫助!由於 現在,一個新的錯誤在web視圖(類) 在功能mostrarpdf(){發佈錯誤 – FactorJose

+0

看起來是這樣的:jobs1.pdf 致命錯誤:意外發現零而展開的可選值 – FactorJose

+0

確定沒有問題,如果答案解決你的問題,請接受它。它可能對另一個有幫助 –