Передача информации из TableViewController в CollectionViewController
У меня довольно простое приложение: есть TableViewController со списком друзей, в ячейке содержится имя и фото. Также есть CollectionViewController в котором также содержится имя и фото. Задача: по нажатию на ячейку в TableViewController отобразить CollectionViewController с именем и фото того друга, который был выбран на предыдущем экране.
Вот код TableViewController
import UIKit
class FriendsViewController: UITableViewController {
var pFriends = friends
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return friends.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FriendCell", for: indexPath) as! FriendTableViewCell
let friendName = friends[indexPath.row].friendName
cell.friendAvatar.image = UIImage(named: friends[indexPath.row].friendAvatar)
cell.friendName.text = friendName
return cell
}
}
Код CollectionViewController
import UIKit
class PhotoViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 1
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCell", for: indexPath) as! PhotoCollectionViewCell
cell.photoName.text = "Фото"
return cell
}
}
Массив с друзьями в отдельном файле
Ответы (1 шт):
Автор решения: Oleg Soloviev
→ Ссылка
Добавьте во FriendsViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "selectFriend", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "selectFriend", let indexPath = sender as? IndexPath, let controller = segue.destination as? PhotoViewController {
controller.friendName = friends[indexPath.row].friendName
controller.friendAvatar = friends[indexPath.row].friendAvatar
}
}
А в PhotoViewController перед viewDidLoad:
var friendName: String?
var friendAvatar: String?
Тогда:
cell.photo.image = UIImage(named: friendAvatar ?? "placeholder")
cell.photoName.text = friendName


