0

I have a tableview setup like shown below. I want the user to only select the checkmark on right side and not the row. User can select multiple checkmarks. Now I want to get the data of all the rows whose checkmark is selected when 'Add Listing' is pressed . Had it been a default cell checkmark, it was easy , but i can't figure out for that one.

Here is the code of TableViewCell

class FacilitiesTableViewCell: UITableViewCell {
         @IBOutlet weak var facility: UILabel!
            @IBOutlet weak var checkMark: UIButton!

            @IBAction func SetCheckMark(_ sender: UIButton) {
            if checkMark.isSelected == true {
                       checkMark.setImage(UIImage(named: "Checkbox"), for: .normal)
                       checkMark.isSelected = false
                        print("Button Pressed")

                   }
                   else {
                       checkMark.setImage(UIImage(named: "UnCheckbox"), for: .normal)
                        checkMark.isSelected = true
             }
        }
    }

Here is the code of my UIViewController Class

class OwnerAddListingFacilitiesViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!

    let Facilities : [String] = ["AC", "Geaser", "Wifi","Telephone", "TV","Kitchen","Cable","Heater"]
    var FacilitiesTaken : [String] = []


    override func viewDidLoad() {
        super.viewDidLoad()
        //tableView.allowsSelection = false

    }

    @IBAction func addListingTapped(_ sender: UIButton) {
        print(FacilitiesTaken)
    }

}

extension OwnerAddListingFacilitiesViewController : UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Facilities.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! FacilitiesTableViewCell
        cell.facility.text = Facilities[indexPath.row]
        cell.SetCheckMark(cell.checkMark)


        return cell
    }

And thats the Image of My tableView Screen

TableView Screen with checkmarks

Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32
Fahad Ali
  • 205
  • 1
  • 9
  • When a cell is checked you need to update this in your data model. You can't rely on the cell itself for state information. It is just a view. [this](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510) might give you some ideas. – Paulw11 May 10 '20 at 20:39
  • That was very Helpful brother! – Fahad Ali May 10 '20 at 21:45

0 Answers0