UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

milestone covering projects 10-12 question. TableView cell won't show images

Forums > 100 Days of Swift

please ignore picker.sourcetype for now. I am still waiting on my ipad to arrive and xcode has refused to work with my iphone.

looking at this code, when I click the add button on the top of left of screen, I can pick images, but those images won't display on the tableView. Is there a reason why my images aren't being shown in a tableView like project 1? I added print statements, and I can see the imageArray is empty which it shoud not be right? From project 10, I remember we didn't need to do any json work just to display the images as grids. Now I know the tableView cell doesn't have an imageView like we had in the collectionView in project 10, but it should still display the image name right? so when I click it I can show it in the detail screen?

First here's my class where


import UIKit

class Person: NSObject, Codable {
    var names: String
    var photos: String

    init(names: String, photos: String) {
        self.names = names
        self.photos = photos

    }
}

and here's the main ViewController


import UIKit

class ViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var imagesArray = [Person]()

    override func viewDidLoad() {

        super.viewDidLoad()
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addPeople))
        print(imagesArray.count)
        // Do any additional setup after loading the view.
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return imagesArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Detail", for: indexPath)
        let Person = imagesArray[indexPath.row]
        let imagePath = getDocumentsDirectory().appendingPathComponent(Person.photos)
        cell.imageView?.image = UIImage(contentsOfFile: imagePath.path)
        cell.textLabel?.text = Person.names
        tableView.reloadData()
        return cell
    }

    @objc func addPeople(){
        let picker = UIImagePickerController()
        picker.sourceType = .photoLibrary
        picker.allowsEditing = true
        picker.delegate = self
        present(picker, animated: true)
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let image = info[.editedImage] as? UIImage else{return}
        let imageName = UUID().uuidString
        let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
        if let jpegdata = image.jpegData(compressionQuality: 0.8){
            try? jpegdata.write(to: imagePath)
        }
        let person = Person(names: "Unknown", photos: imageName)

        imagesArray.append(person)

        tableView.reloadData()
        print(imagesArray.count)
        dismiss(animated: true)
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Hello")
    }

    func getDocumentsDirectory() -> URL{
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return paths[0]
    }

}

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.