tableview how to append to empty array checked cells?

class Cell: UITableViewCell {

    @IBOutlet weak var checkBtn     : UIButton!


    @IBAction func selectButton(_ sender: UIButton) {
        if checkBtn.isSelected == false {
            checkBtn.isSelected = true
        } else {
            checkBtn.isSelected = false
        }
    }
}

I have a cell class. But the IBACtion does not work.

I also make a ViewController like below

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)   -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "exampleCell") as! Cell

    if cell.checkBtn.isSelected == true {
            anEmptyArray.append(myTableViewData[indexPath.row])
        } else {

        }

}

How I can get selected cells?

1 answer

  • answered 2022-01-20 17:09 Aafaq

    You can use the following table view delegate method, below is the code.

    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
         anEmptyArray.append(myTableViewData[indexPath.row])
    }
    
    

    Also make sure you have following line of code in your viewDidLoad method.

     tableView.delegate = self
    

    With this approach you have to just select the cell. That's all.

    Another Approach

    You need to create new delegate.

    protocol TableViewCellDelegate: AnyObject {
       func didSelect(cell: YourTableViewCell) // Don't need to define just declare.
    }
    

    Now create a delegate instance in UITableViewCell class, and call method inside button action.

    class Cell: UITableViewCell {
    
        @IBOutlet weak var checkBtn     : UIButton!
        weak var delegate: TableViewCellDelegate? // Instance of Delegate protocol
    
    
        @IBAction func selectButton(_ sender: UIButton) {
            if checkBtn.isSelected == false {
                checkBtn.isSelected = true
            } else {
                checkBtn.isSelected = false
            }
            
            self.delegate?.didSelect(cell: self) // triggering the delegate method. 
        }
    }
    
    

    Now which code will be trigger? We need to define that. Go to your UITableViewController, and extend that class, for example

    extension YourTableViewController: TableViewCellDelegate {
         func didSelect(cell: YourTableViewCell) {
           // Now you have the cell, and you can get the indexPath of this cell
             if let indexPath = tableView.indexPath(for: cell) {
                // Now you have indexPath and you know the drill.
                 
    
                // If you want to remove that cell delete that value from array and reload tableView.
                
                myTableViewData.remove(at: indexPath.row)
                tableView.reloadData()
    
                
             }
         }
    }
    

    Don't forget making the cell delegate of YourTableViewController like below.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)   -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "exampleCell") as! YourTableViewCell
    
        cell.delegate = self
    
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum