Abhijeet Abhijeet - 1 month ago 15
Swift Question

Is it possible to add a tick mark(image) to my TableView cell when a button is tapped in other screen after tableView didselect method?

I have a tableview which Contains 10 cell named one to ten respectively. When I select a cell a new screen opens which contains a button. I want to know is it possible when I tap that button the tableView cell related to that button gets a tick mark image? if yes then how?

Answer

By "new screen" do you mean a new view controller? You can do it by the Delegate pattern.

protocol MainViewControllerDelegate {
    func buttonOnClick(indexPath: NSIndexPath)
}

class MainViewController: UIViewController, MainViewControllerDelegate {

    func buttonOnClick(indexPath: NSIndexPath) {

        guard let cell = tableView.cellForRowAtIndexPath(indexPath) else {
            return
        }

        cell.accessoryType = .Checkmark
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

        let newViewController = NewViewController()
        newViewController.delegate = self
        newViewController.indexPath = indexPath
        self.presentViewController(newViewController, animated: false, completion: nil)
    }
}

class NewViewController: UIViewController {

    var delegate: MainViewControllerDelegate!
    var indexPath: NSIndexPath!

    func buttonOnClick() {
        delegate.buttonOnClick(indexPath)
    }
}
Comments