简体   繁体   中英

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

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

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
    }
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM