好吧,我將演示如何使用靜態字符串數組來完成此操作。 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
}
}
第二視圖控制器圖像上的收藏視圖和標籤文本將會改變取決於您的表格行點擊第一個視圖控制器? –
沒錯,我需要那個 – JuanCa2607