Search code examples
iosswiftxcodeuitableviewdidselectrowatindexpath

DidSelectRow is not working


Pretty much what the title says: DidSelectRow is not getting called, and I did check that I didn't use Deselect. I also checked that delegate and DataSource are connected to the tableViewController and the Selection was Single Selection. However, the method is still not getting called.

Does an empty tableView might have any effect to it(reason it's empty is because the user starts to populate it. I'm using CoreData for it)?

EDIT

This is what the method looks like:

 var effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    var effect: UIVisualEffect!
    let defaults = UserDefaults.standard
    var userMagazineTitle = [User]()
    var dataString = String()

override func viewDidLoad() {
        super.viewDidLoad()
        addProgrammatically()

        let fetchRequest: NSFetchRequest<User> = User.fetchRequest()
        do{
            let users = try PresistanceService.context.fetch(fetchRequest)
            self.userMagazineTitle = users
            self.tableView.reloadData()
        }catch{
            ProgressHUD.showError("Nothing to see here")
        }

    } 

 / MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return userMagazineTitle.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyFeedTableViewCell
        cell.myHeadline?.text = userMagazineTitle[indexPath.row].title
        cell.indentationLevel = 3
        return cell
    }

    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        let manage = PresistanceService.persistentContainer.viewContext
        let del = userMagazineTitle[indexPath.row]

        if editingStyle == .delete {
            // Delete the row from the data source
            manage.delete(del)
            do{
                try manage.save()
            }catch{
                ProgressHUD.showError()
            }

            let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")

            do{
                userMagazineTitle = try manage.fetch(fetchRequest) as! [User]
            }catch{
                ProgressHUD.showError()
            }
            tableView.reloadData()
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "showUser", sender: indexPath)
        navigationController?.navigationBar.isHidden = false
        tableView.deselectRow(at: indexPath, animated: true)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showUser" {
            let DestViewController = segue.destination as! UINavigationController
            let targetController = DestViewController.topViewController as! CompanyTableViewController
            let indexPath = sender as! IndexPath
            let user = userMagazineTitle[indexPath.row].title
            let user2 = userMagazineTitle[indexPath.row].rssurl
            targetController.customUserInit(articlesindex: indexPath.row, title: user!, rssUrl: user2!)
        }

    }

    func addProgrammatically() {
        effectView.frame = view.bounds
        tableView.addSubview(effectView)

        effectView.translatesAutoresizingMaskIntoConstraints = false
        if #available(iOS 11.0, *) {
            effectView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        } else {
            // Fallback on earlier versions
            effectView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
        }
        effectView.heightAnchor.constraint(equalTo: self.view.heightAnchor).isActive = true
        effectView.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true

        effect = effectView.effect
        effectView.effect = nil

        self.tableView.separatorStyle = .none
        tableView.backgroundView = UIImageView(image: UIImage(named: "myMagazines.jpg"))
        tableView.backgroundView?.contentMode = .scaleAspectFill
        tableView.clipsToBounds = true
        navigationController?.navigationBar.isHidden = false
        UIApplication.shared.statusBarStyle = .lightContent
        navigationController?.navigationBar.isTranslucent = false
        navigationController?.navigationBar.barStyle = .black
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    }

}

Solution

  • You have added a UIVisualEffectView view on top of tableview, which is not passing the touch events to tableview.

    Setting effectView.isUserInteractionEnabled = false, should make this view pass touch events to the views underneath.