'How can I show the Text and Label in the CityDetailVC in swift?
Cities
import Foundation
struct City {
var image:String = ""
var name:String = ""
var text:String = ""
init(image: String, name: String, text: String){
self.image = image
self.name = name
self.text = text
}
}
CityCollectionView
class CityCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var cityImageView: UIImageView!
@IBOutlet weak var cityNameLabel: UILabel!
}
CityCollectionVC
import UIKit
private let reuseIdentifier = "Cell"
class CityCollectionViewController: UICollectionViewController {
@IBAction function unwindToMain(segue: UIStoryboardSegue){
}
private var cities : [City] = [ City(image: "America", name: "America"),
City(image: "Australia", name: "Australia"),
City(image: "Austria", name: "Austria"),
City(image: "Bangladesh", name: "Bangladesh"),
City(image: "Belgium", name: "Belgium"),
City(image: "Canada", name: "Canada"),
City(image: "China", name: "China"),
City(image: "Czech Republic", name: "Czech Republic"),
City(image: "France", name: "France"),
City(image: "Germany", name: "Germany"),
City(image: "Hungary", name: "Hungary"),
City(image: "India", name: "India"),
City(image: "Korea", name: "Korea"),
City(image: "Luxembourg", name: "Luxembourg"),
City(image: "Netherlands", name: "Netherlands"),
City(image: "Russia", name: "Russia"),
City(image: "Slovakia", name: "Slovakia"),
City(image: "Switzerland", name: "Switzerland"),
City(image: "italy", name: "Italy"),
City(image: "UK", name: "United States"),
City(image: "Vietnam", name: "Vietnam")]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: UICollectionViewDataSource
override function numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override function collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return cities.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataCell", for: indexPath) as! CityCollectionViewCell
// Configure the cell
let city = cities[indexPath.row]
cell.cityImageView.image = UIImage(named: city.image)
cell.cityNameLabel.text = city.name
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: nil)
}
override function prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPaths = collectionView.indexPathsForSelectedItems{
let destinationController = segue.destination as! CityDetailViewController
destinationController.city = cities[indexPaths[0].row]
collectionView.deselectItem(at: indexPaths[0], animated: false)
}
}
}
}
CityDetailVC
import UIKit
class CityDetailViewController: UIViewController {
@IBOutlet var cityImageView:UIImageView!
@IBOutlet weak var cityTextView: UITextView!
@IBOutlet weak var cityLabel: UILabel!
var city: City?
var textView = ""
var label = ""
override func viewDidLoad() {
super.viewDidLoad()
// cityImageView.image = UIImage(named: city?.image ?? "")
cityImageView.image = UIImage(named: city?.image ?? "")
}
}
I'm a beginner in swift. I don't know how to show the label and text in city detailVC. I tried but the thing is image is only showing in city detailVC not the label and text. Please guide me and give some example codes. I don't know where to use the label and text. I didn't gave any label and text (content) for showing it in detail VC.
Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
