2016-08-05 196 views
1

mi問題是這樣的:我想選擇第一,第二...行,並且視圖控制器必須根據所選行(標籤,圖像,等等),我嘗試了很多東西,但沒有工作。將表視圖(行選擇)的數據傳遞到視圖控制器

我使用協議,這一點:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 4 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF 


    cell.l2.text = hname[indexPath.row] 
    cell.l1.text = prename[indexPath.row] 
    cell.img1.image = imgs[indexPath.row] 

    return cell 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 
    let selectedH = self.nin2[(indexPath as NSIndexPath).row] 
    self.delegate?.HabiSelected(selectedH) 
} 

感謝

enter image description here

+0

第二視圖控制器圖像上的收藏視圖和標籤文本將會改變取決於您的表格行點擊第一個視圖控制器? –

+1

沒錯,我需要那個 – JuanCa2607

回答

1

好吧,我將演示如何使用靜態字符串數組來完成此操作。 Analise它使你的代碼你需要什麼(圖片,文字)

步驟1:創建新的項目,並選擇視圖 - 控制轉到 - >嵌入在 - >導航控制器。然後將UITableView添加到您的ViewController。然後添加一個原型單元格並命名它(例如.cell)。

第2步:創建新的UITableViewCell(newcell)可可觸摸類文件。

第3步:然後轉到identity->自定義類 - >放入創造UITableViewCell可可觸摸類文件到您的原型細胞(細胞)。然後將UILabel添加到您的prototye單元中。

第4步:創建表視圖委託給視圖控制器,並創建出口標籤到新單元,然後創建表視圖來查看控制器。

第5步:添加新的UIViewController你的故事板進行的CollectionView和遵循UITableView

步驟6類似的方法:創建從視圖 - 控制推到Segue前收集的ViewController並命名SEGUE。

之後,將以下代碼添加到具有表視圖的視圖控制器。

import UIKit 

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ 


@IBOutlet var tableView: UITableView! //Outlets of table View 

var titles = ["Michael Jackson","Taylor Swift","Katy Perry","Avril Lavigne"] //static Array 

override func viewDidLoad() { 
super.viewDidLoad() 

} 

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableCell 
cell.TitlesLabel.text = titles[indexPath.row] 
return cell 
} 

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

performSegueWithIdentifier("collectionSegue", sender: self) 
} 

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

let index = self.tableView.indexPathForSelectedRow 
let indexNumber = index?.row //0,1,2,3 
let vc = segue.destinationViewController as! collectionViewController 
vc.title = titles[indexNumber!] //NavigationItem.title on collectionViewController 
} 

override func didReceiveMemoryWarning() { 
super.didReceiveMemoryWarning() 

} 
} 

添加下面的代碼到您的收藏視圖控制器。

import UIKit 

class collectionViewController: UIViewController, UICollectionViewDataSource,UICollectionViewDelegate{ 


@IBOutlet var collectionView: UICollectionView! 

var collectionTitles = ["Hi","Hello","Hola","Ni hao"] 
var taylorSwift = ["Speak Now","Red","Love Story","Blank Space"] 
var thirdlabel = ["Jack","Sparrow","William","Mohan Singh"] 
var fourthlabel = ["Namasta","Vanakkam","Tamil","Hindi"] 


override func viewDidLoad() { 
super.viewDidLoad() 

} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
return taylorSwift.count 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! CollectionViewControllerCell 
let identity = navigationItem.title 
print(title) 

if identity == "Michael Jackson" 
{ 
cell.collectionLabel.text = collectionTitles[indexPath.row] 
} 
else if identity == "Taylor Swift" 
{ 
cell.collectionLabel.text = taylorSwift[indexPath.row] 
} 
else if identity == "Katy Perry" 
{ 
cell.collectionLabel.text = thirdlabel[indexPath.row] 
} 
else 
{ 
cell.collectionLabel.text = fourthlabel[indexPath.row] 
} 
return cell 
} 
} 
+0

是的,這是我所需要的。非常感謝:D – JuanCa2607

+0

不用客氣:);) –

0

你應該編程爲下一個視圖控制器,你現在創建的實例和信息傳遞給這樣的視圖控制器所以

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

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCellF 
    let nextVC = self.storyboard?.instantiateViewControllerWithIdentifier("your identifier") as! YourCustomViewController 
    nextVC.yourVariable = cell.theInfoYouWhatToPass 

    self.presentViewController(nextVC, animated: true, completion: nil) 
} 

現在你的下一個VC實例將有你需要的信息存儲在你的變量中

+0

我嘗試過,但是我得到信號SIGABRT – JuanCa2607

相關問題