2016-06-09 49 views
0

我有一個表格視圖,其中有許多單元格。每個單元格都有一個圖像和一個標籤。我需要的是,當用戶按下任何單元格時,它必須轉到詳細信息viewcontroller.And那裏我需要在我的詳細視圖控制器中顯示各自的圖像和標籤名稱。如何做到這一點。如何將表格視圖單元格中的值傳遞給視圖控制器

我已經完成了所有segue.But在我的詳細信息視圖控制器我有一個圖像和label.Now如何顯示圖像和標籤名稱 - 當我從我的表視圖中選擇任何單元格?

import UIKit 

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 

    @IBOutlet var tableView: UITableView! 
    var valueToPass:String! 
var selectedIndexPath: NSIndexPath = NSIndexPath() 

    var tableData: [String] = ["Christ Redeemer", "Great Wall of China", "Machu Picchu","Petra","Pyramid at Chichén Itzá","Roman Colosseum","Taj Mahal"] 
    var arrImageName: [String] = ["ChristRedeemer", "GreatWallOfChina", "MachuPicchu","Petra","PyramidChichenItza","RomanColosseum","TajMahal"] 

var tableRate: [String] = ["$120", "$100", "$222","$1000","$500","$900","$2000"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

    } 

    func numberOfSectionsInTableView(tableView: UITableView) ->Int 
    { 
    return 1 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.tableData.count; 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as! CustomTableViewCell 

     cell.imageVW.image = UIImage(named:self.arrImageName[indexPath.row]) 

     cell.lblName.text = self.tableData[indexPath.row] 
     cell.rateName.text = self.tableRate[indexPath.row] 

     return cell 
    } 


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     self.selectedIndexPath = indexPath 

     performSegueWithIdentifier("DetailView", sender: indexPath) 
    } 


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ 
     let indexPath = self.selectedIndexPath 

     if (segue.identifier == "DetailView") { 
      var viewController = segue.destinationViewController as! DetailVC 
      viewController.data = UIImagePNGRepresentation(UIImage(named:self.arrImageName[indexPath.row])!)! 
      //viewController.name = self.tableData[[indexPath.row]] 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

這裏是我的detailvc.swift

import UIKit 

class DetailVC: UIViewController { 


    var data: NSData = NSData() 
    var name: String = String() 



    @IBOutlet weak var ImgView: UIImageView! 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.ImgView.image = UIImage(data: data) 
     //self.detailLabelNamee.text = name 
    } 






} 

回答

3

改變你做選擇的是這樣

中聲明ViewContrller一個gloabal indexPath像這樣

var selectedIndexPath: NSIndexPath = NSIndexPath() 

更改didselect這樣

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.selectedIndexPath = indexPath 
    self.performSegueWithIdentifier("DetailView", sender: self) 
} 

現在prepareForSegue方法添加此

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ 
    let indexPath = self.selectedIndexPath 

    if (segue.identifier == "DetailView") { 
     var viewController = segue.destinationViewController as! DetailVC 
     viewController.data = UIImagePNGRepresentation(UIImage(named:self.arrImageName[indexPath.row])) 
     viewController.name = self.tableData[indexPath.row] 
    } 
} 

現在DetailVC添加兩個全局識別符像下面

var data: NSData = NSData() 
var name: String = String() 

現在,將這個數據和字符串imageview和標籤in viewdid加載

像這樣改變detailVC的viewdidload

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.ImgView.image = UIImage(data: data) 
    self.detailLabelNamee.text = name 
} 

跳這將有所幫助。

+0

我得到錯誤'的實例成員「數據」不能在類型「DetailVC」' – mark

+0

但可以使用我怎樣才能分配圖像,標籤名稱我細節視圖控制器。因爲當我選擇任何單元格時,我需要在我的下一個詳細視圖控制器中顯示特定的單元格圖像和名稱。所以,只有我做了iboutlet的圖像,標籤到我的詳細視圖控制器。現在,我做了你的解決方案。但它只是創建一個變量。我可以如何在我的詳細視圖控制器中分配圖像,標籤名稱? – mark

+0

我已編輯我的答案並添加viewdidload。 –

0

可以聲明一個變量在那家商店indexpath。

VAR selectedItemIndex = INT()

下面線路單元寫的點擊在didselectowatndexaPath

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

     selectedItemIndex = indexPath.row 
     self .performSegueWithIdentifier(「YourViewController」, sender: nil) 
    } 


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

     let yourVC = segue.destinationViewController as! YourViewController 


     yourVC.text = tableData[selectedItemIndex] 
     yourVC.image = UIImage(named:arrImageName[selectedItemIndex]) 
    } 
0

你快到了!該行: performSegueWithIdentifier(「DetailView」,發件人:自己)

需要更改。 'self'將當前視圖控制器的引用傳遞給prepareForSegue方法。如果將'self'更改爲'indexPath',那麼prepareForSegue將在'sender'對象中看到單元格索引路徑,並從中獲取所需內容。

你可以這樣做:

if (segue.identifier == "yourSegueIdentifer") { 
    var viewController = segue.destinationViewController as! DetailVC 
    let indexPath = sender as! NSIndexPath 
    viewController.passedImageName = self.arrImageName[indexPath.row] 
    viewController.passedData = self.tableData[indexPath.row] 
    viewController.passedRate = self.tableRate[indexPath.row] 

和DetailVC:

class DetailVC: UIViewController { 
    var passedImageName:String! 
    var passedData:String! 
    var passedRate:String! 
+0

但是我如何在我的詳細視圖控制器中分配圖像,標籤名稱。因爲當我選擇任何單元格時,我需要在我的下一個詳細視圖控制器中顯示特定的單元格圖像和名稱。 ..所以只有我做了iboutlet圖像,標籤到我的詳細視圖控制器。現在我做了你的解決方案。但它只是創建一個變量。我可以如何分配圖像,標籤名稱在我的詳細視圖控制器? – mark

+0

我得到這個錯誤崩潰'無法將類型'SimpleTable.CustomTableViewCell'(0x106060908)的值轉換爲'NSIndexPath'(0x10686b438).'在該行下'let indexPath = sender as! NSIndexPath' – mark

1

在你didSelectRowAtIndexPath你可以執行SEGUE和prepareForSegue你可以用self.tableView.indexPathForSelectedRow得到indexPath。當然,它會返回一個可選項,您必須首先檢查它是否安全。我認爲這是最簡單的方法。

0

在你的didSelectRowAtIndexPath你可以得到所選行的索引路徑,然後你可以在sender中執行Segue和傳遞索引路徑。 在prepareForSeque中,從索引路徑中,可以獲取圖像tableData和tableRate。您可以將其傳遞給詳細視圖。類似下面

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    performSegueWithIdentifier("DetailView", sender: indexPath) 
} 



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ 
    let selectedIndexPath = sender as! NSIndexPath 

    if (segue.identifier == "DetailView") { 
     var viewController = segue.destinationViewController as! DetailVC 

viewController . setValues(tableData:tableData[selectedIndexPath] , rate: tableRate[selectedIndexPath] , imageName:arrImageName[selectedIndexPath]) 
} 


class DetailVC: UIViewController { 

    var imageName:String! 
    var tableData:String! 
    var tableRate:String! 



func setValues(tableData:String , rate:String , imageName:String){ 
imageName = tableData 
tableRate = rate 
imageName = imageName 

} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ 
    let selectedIndexPath = sender as! NSIndexPath 

    if (segue.identifier == "DetailView") { 
     var viewController = segue.destinationViewController as! DetailVC 

viewController.imageName = self.arrImageName[selectedIndexPath.row] 
viewController.tableData = self.tableData[selectedIndexPath.row] 
viewController.tableRate = self.tableRate[selectedIndexPath.row] 
} 
相關問題