This is my solution for this challenge, it took me some time, but I have just replaced the current "UITableViewController" with "UICollectionViewController" than I changed that in interface builder as well but I left the "UIViewController/ DetailViewcontroller(this is the custom class we created from before to display the image in a much bigger format on a different screen)" I'm still tryna figure out how I can display the images straight away in collection view cells, but this works as well( is just a version). So this is my code:
Make sure you create a custom class for your collection view cell like we did in project 10, open the assistant editor and just connect the imageView and the label to IB(interface builder) also make sure you go to identity inspector and select its class as your custom class you just created in order to be able to connect your code to IB. Leave "DetailviewController" class as it is and make sure you don't delete that UIViewController from interface builder. I also made those cells nice with round corners and with a nice ligh grey background and I also made the "Collection View" background black, looks cool, I also changed the colour of my Navigation "title" to white as the whole backgrounk is black, but you can change it to whatever colour you like. Any suggestions are welcomed! Hope this helps.Have fun!
import UIKit
class ViewController: UICollectionViewController {
var pictures = [String]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Storm Viewer"
navigationController?.navigationBar.prefersLargeTitles = true
let fm = FileManager.default
let path = Bundle.main.resourcePath!
let items = try! fm.contentsOfDirectory(atPath: path)
for item in items {
if item.hasPrefix("nssl") {
pictures.append(item)
}
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictures.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Picture", for: indexPath) as! PictureCell
cell.textLabel?.text = pictures[indexPath.row]
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.borderWidth = 2
cell.layer.cornerRadius = 7
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
vc.selectedImage = pictures[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
}
}