UItableview didSelectRowAtIndexPath not being called

I am working on show weather project. I am listing the cities from database. When I click on a city, another viewcontroller should open. But when I click on a city, the

didSelectRowAtIndexPath
function does not work.

Here is the my code:

import UIKit
import SCLAlertView
import Popover
class UserInfoViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    fileprivate var texts = ["Los Angeles", "New York", "Chicago"]

    fileprivate var popover: Popover!
    fileprivate var popoverOptions: [PopoverOption] = [
      .type(.auto),
      .blackOverlayColor(UIColor(white: 0.0, alpha: 0.6))
    ]
    @IBOutlet var compositionNum: UILabel!
    @IBOutlet var fans: UILabel!
    @IBOutlet var location: UILabel!
    @IBOutlet var userName: UILabel!
    @IBOutlet var userImag: UIImageView!
    @IBOutlet var imagBt: UIButton!
    @IBAction func modifyInfo(_ sender: Any) {
        let alert = SCLAlertView()
        let textField1 = alert.addTextField(userName.text)
        let textField2 = alert.addTextField(location.text)
        alert.addButton(“Done”)
        }
    }
    @IBOutlet var topButton: UIButton!
    @IBAction func expandSet(_ sender: Any) {
        
        self.popover = Popover(options: self.popoverOptions)
                let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width-250, height: 120))
        tableView.delegate = self
        tableView.dataSource = self
        tableView.isScrollEnabled = false
        tableView.allowsSelection = true
        self.popover.show(tableView, fromView: self.topButton)
    }
    
    @IBAction func modifyImag(_ sender: Any) {
        showBottomAlert()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.bringSubviewToFront(userImag)
        view.bringSubviewToFront(imagBt)
    }
    func photos()  {
      self.showBottomAlert()
    }
    func showBottomAlert(){
        let alert = SCLAlertView()
        
    }
    func goCamera(){
                    
        if UIImagePickerController.isSourceTypeAvailable(.camera){
            let  cameraPicker = UIImagePickerController()
            cameraPicker.delegate = self
            cameraPicker.allowsEditing = true
            cameraPicker.sourceType = .camera
            self.present(cameraPicker, animated: true, completion: nil)
        } else {
            SCLAlertView().showError("",subTitle: "",closeButtonTitle: "")
        }

    }
    func goImage(){
        let photoPicker =  UIImagePickerController()
        photoPicker.delegate = self
        photoPicker.allowsEditing = true
        photoPicker.sourceType = .photoLibrary
        self.present(photoPicker, animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        let image : UIImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
        userImag.image = image
        userImag.layer.cornerRadius = userImag.frame.size.width / 2
        self.dismiss(animated: true, completion: nil)
    }
}
extension String{
    var isBlank:Bool{
        return allSatisfy({$0.isWhitespace})
    }
}
extension UserInfoViewController: UITableViewDelegate {

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.popover.dismiss()
  }
}

extension UserInfoViewController: UITableViewDataSource{

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

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
    cell.textLabel?.text = self.texts[(indexPath as NSIndexPath).row]
    return cell
  }
  func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
        if(indexPath.row == 0){
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            guard let secondVC = storyboard.instantiateViewController(withIdentifier: "Main") as? MainViewController else  {  return }
             self.present(secondVC, animated: true, completion: nil)
        }
        else if(indexPath.row == 1){
            
        }
        else{
            
        }
    }
}

What should I do to make it correct?

Thanks in advance.

Accepted Reply

Normal.


I did not notice you declare this func in UITableViewDataSource.

It has to be in UITableViewDelegate only, and it is already there.


Should move the code in UITableViewDelegate.

Replies

The delegate func signature is wrong.


  func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {


It should be


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

After revising the func there is an error:

" Invalid redeclaration of 'tableView(_:didSelectRowAt:)' "

Normal.


I did not notice you declare this func in UITableViewDataSource.

It has to be in UITableViewDelegate only, and it is already there.


Should move the code in UITableViewDelegate.

Thanks,that helps me a lot.

I have the same problem. Even though I use the correct declaration. The cells have only a label and image view. The table is downloading data from a rest api.
Could anyone hekp me?
Thanks in advance.

You should better start a new thread with showing your code.