我想问如何在tableView和webView之间正确发送pdf。这里我有两个字符串分别是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)
}
}
根据Apple:
sender:发起segue的对象。您可以使用这个参数来执行基于哪个控件(或其他对象)发起segue的不同操作。
segue可以从多个源触发,所以使用segue和sender参数中的信息来消除应用程序中不同逻辑路径之间的歧义。例如,如果segue起源于表视图,sender参数将识别用户单击的单元格。你可以使用那个信息来设置目标视图控制器上的数据。
你的问题是sender
不用于你想要的。但是你在didSelectRowAtIndexPath
和indexPath.row
中有一个全局变量pdfSeleccionado
,你可以用它来尝试在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]
}
}