Displaying items from a tableView that is checkmarked

Below I have my code where I have a button to randomly display names on a label from a list on a tableview. Im trying to only display the names that have been checkmarked on the tableview. I would appreciate the help, Thank you in advance.



import UIKit

class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
    let names = ["Eric","Matt","Amy","Chris","John"]
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var mainLabel: UILabel!
    override func viewDidLoad() {
        tableView.delegate = self
        tableView.dataSource = self
        // Do any additional setup after loading the view.
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        let text = names[indexPath.row]
        cell?.textLabel?.text = text
        return cell!
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCell.AccessoryType.none{
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.checkmark
            tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.none

    @IBAction func randomButton(_ sender: Any) {
        self.mainLabel.text = self.names.randomElement()

Powered by WPeMatico

You may also like...

Comments are closed.