0

I created 5 cells in my tableView. My first cell is imageCell and it consist of two parts. One is userImageView and the other is userNameText. I did all instructions such as writing delegate protocol and the others. I did not get any error, build was succeed but it did not work. I realized that I did not define my userImageView at cellForRowAt indexPath 0. I just only defined userNameText. I thought that it would be possibly reason. I tried to define as you seen the bottom. But there is a problem. if I equalize UIImage() cod, I don't see anything at the cell and I can not tap anything. if I write nothing about userImageView, I can see the image from Assets assigned and I can tap but nothing happen. I will be very happy if someone helps me..

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
        cell.userImageView.image = UIImage()
        cell.userNameText.text = ""
        return cell

Here is my UITableViewController

import UIKit
protocol ImagePickerDelegate {
func pickImage()
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
        cell.userImageView.image = UIImage()
        cell.userNameText.text = ""
        return cell
    } else if indexPath.row == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TCNumberCell") as! TCNumberCell
        cell.TCnumberText.text = ""
        return cell
    } else if indexPath.row == 2 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "birthdayCell") as! birthdayCell
        cell.birthdayText.text = ""
        return cell
    } else if indexPath.row == 3 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "companyCell") as! companyCell
        cell.companyNumberText.text = ""
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "schoolCell") as! schoolCell
        cell.schoolNumberText.text = ""
        return cell
    }

}


@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    tableView.delegate = self
    tableView.dataSource = self

}

func pickImage() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
    imagePicker.allowsEditing = false
    self.present(imagePicker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
        let cell:imageCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! imageCell
        cell.userImageView.image = image
    }

    picker.dismiss(animated: true, completion: nil)
    }
}

and here is my imageCell

import UIKit

class imageCell: UITableViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


@IBOutlet weak var userNameText: UITextField!
@IBOutlet weak var userImageView: UIImageView!

var delegate : ImagePickerDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    addTapGestureRecognizer(for: userImageView)
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}

func addTapGestureRecognizer(for view: UIImageView) {
    let tap = UITapGestureRecognizer(target: self, action: #selector(pickImage))
    tap.numberOfTapsRequired = 1
    view.isUserInteractionEnabled = true
    view.addGestureRecognizer(tap)
}

@objc func pickImage() {
    delegate?.pickImage()
  }
}

tableView

Marcy
  • 4,611
  • 2
  • 34
  • 52

2 Answers2

0

The error is because UITableViewCell has no function present. You should have a callback to the UIViewController that's displaying this table view cell, and have it present the UIImagePickerController with presentViewController(imagePicker, animated: true, completion: nil).

If you're not sure how to do that callback, see this answer here: https://stackoverflow.com/a/22904272/4608154

Essentially, you create a protocol in your TableViewCell class, and have your ViewController assigned as the delegate. Then when your TableViewCell wants to display the UIImagePickerController, the ViewController will have a method triggered.

johnny
  • 1,434
  • 1
  • 15
  • 26
0

You assigned an empty image to imageView in a cell. Check table view delegate function in your existing code.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
        cell.userImageView.image = UIImage()

Follow each instruction below

Add a variable for the selected image in ViewController.

class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var selectedImage = UIImage(named:"placeholder") // use as variable. add placeholder image if require

Update the reference cell.userImageView.image. Reference selected image while dequeuing the cell.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
        cell.userImageView.image = selectedImage
        cell.userNameText.text = ""
        return cell

Update the selected image variable once the user picks an image from the picker. Make sure the IF condition should be true to assign an image.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            self.selectedImage = image
            let cell:imageCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! imageCell
Alizain Prasla
  • 744
  • 2
  • 15
  • 37