我想問如何在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)
}
}
爲什麼不使用全局變量'pdfSeleccionado'而不是在'prepareForSegue'中傳遞索引? –
感謝您的兄弟。這是因爲我需要更多時間來練習和理解XCode是如何工作的。但這只是時間問題 – FactorJose