Хорошо, я покажу, как это сделать со статическим массивом строки. Analise это сделать код, что вам нужно (изображение, текст)
Шаг 1: Создайте новый проект и выберите ViewController Гото -> Вставить> не- Навигационный контроллер. Затем добавьте UITableView
к вам ViewController
. Затем добавьте одну ячейку прототипа и назовите ее (например, ячейку).
Шаг 2: Создать новый (newcell) файл класса какао.
Шаг 3: Затем перейти использованием личных данных> Пользовательский класс -> добавить созданный UITableViewCell
какао сенсорный файл класса для вашего прототипа ячейки (клетки). Затем добавьте UILabel
в свою прототие.
Шаг 4: Создайте делегат представления таблицы для просмотра контроллера и создания точек для метки для новой ячейки, а затем для просмотра таблицы.
Шаг 5: Добавить UIViewController
в свою раскадровку для CollectionView и следовать подобному методу UITableView
Шага 6: Создать нажимной SEGUE из ViewController в коллекцию ViewController и имя SEGUE.
После этого, добавьте ниже код к контроллеру вашего вида с таблицей View.
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