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()
}
}