George Heints
George Heints

Reputation: 1435

Swift 4 Switch relate to label on tableView

I have problem to get label from cell when i turn my switch ON. I do fetch all labels from Firebase Database.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "tagCell", for: indexPath) as! TagsTableViewCell
    print(myCallList[indexPath.row])
    let _tag = myCallList[indexPath.row]
    cell.tagLabel?.text = _tag.type

    return cell
}

enter image description here

UPDATED: UITableViewCell contain nothing special

import UIKit

class TagsTableViewCell: UITableViewCell {

    @IBOutlet weak var tagLabel: UILabel!
    @IBOutlet weak var tagSwitch: UISwitch!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

My model:

class Calls: NSObject {
    var type: String?

    init(type: String?) {
        self.type = type
    }
}

LoadCalls contain Firebase data fetch:

func LoadCalls() {
        ref = Database.database().reference()
        let userID = Auth.auth().currentUser?.uid
        self.myCallList.removeAll()
        ref.child("tags").observe(.childAdded, with: { (snapshot) in
            if snapshot != nil{
                var tagType = snapshot.key as? String
                let myCalls = Calls(type: tagType)
                self.myCallList.append(myCalls)
                print(self.myCallList.count)
                DispatchQueue.main.async {
                    self.tagsTableView.reloadData()
                }
            }
        })
    }

Upvotes: 0

Views: 162

Answers (2)

Alexander B
Alexander B

Reputation: 137

I guess it something like this - add tag to switcher and create action for it

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "tagCell", for: indexPath) as! TagsTableViewCell
    print(myCallList[indexPath.row])
    let _tag = myCallList[indexPath.row]
    cell.tagLabel?.text = _tag.type
    cell.switcher.tag = indexPath.row

    return cell
}

And after this

@IBAction func switcherChanged(_ sender: UISwitch) {
   var getLabel = myCallList[(sender as AnyObject).tag]
   print(getLabel.type)
}

Upvotes: 0

Augie
Augie

Reputation: 1341

A delegate / protocol for communication between cell and table controller can work well here.

protocol switchCellDelegate : Class {
     func cellSwitchChanged( value: String, sender: Any)
}

update table view cell with property and IBAction for switch change

class TagsTableViewCell: UITableViewCell {

     weak var delegate : switchCellDelegate?

     @IBAction func switchChanged(sender: UISwitch){
         guard let delegate = delegate else { return }
         if sender.isOn {
              delegate.cellSwitchChanged( value: tagLabel.text, sender: self)
          }
     }

and then in cellForRowAtIndex, add this

cell.delegate = self

and controller

extension myController : switchCellDelegate {
     func cellSwitchChanged( value: String, sender: Any){
         //do what you want here
     }
}

Upvotes: 1

Related Questions