Trigger an action when cell in static UITableView is tapped [Swift]

5k views Asked by At

I have eight, different static cells in a tableview. I am trying to perform different actions with six of the cells. Two of them open an alert view, two open safari, one clears a cache, and the last one opens a share popover. Previously, they all worked with UIButtons and IBActions. But since setting up a static tableview, I am running into issues.

I'm trying to link up the individual cells to their own actions and I can't seem to do that with outlets. How might I do this?

Thank you.

Swift 3

Table View code:

import UIKit

class AltSettingsTableViewController: UITableViewController {

@IBOutlet weak var copyrightInfo: UITableViewCell!

@IBOutlet weak var shareApp: UITableViewCell!

@IBOutlet weak var rateApp: UITableViewCell!

@IBOutlet weak var clearCache: UITableViewCell!

@IBOutlet weak var purchasePhotos: UITableViewCell!

@IBOutlet weak var helpFeedback: UITableViewCell!

override func viewDidLoad() {
    super.viewDidLoad()


    self.tableView.backgroundColor = UIColor.clear
    self.tableView.backgroundView = nil;

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return CGFloat.minimum(35, 35)
}

override var prefersStatusBarHidden: Bool {
    get {
        return true
    }
}
1

There are 1 answers

7
Samantha On BEST ANSWER

Have you overridden func tableView(UITableView, didSelectRowAt: IndexPath)? It should let you know when a cell is tapped and then you can use indexPath to determine which cell it was.

Edit: documentation for UITableViewDelegate and NSIndexPath.

Example:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        // do stuff
    }
    //...
}