1
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tbl_vw.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as!cuscellTableViewCell

    cell.txt_lbl.text = self.section[indexPath.row];
    cell.sel_btn.addTarget(self, action: #selector(self.switchChanged), forControlEvents: .ValueChanged)

    return cell

}



func switchChanged(sender: AnyObject) {
    let switchControl: UISwitch = sender as! UISwitch
    print("The switch is \(switchControl.on ? "ON" : "OFF")")

    if switchControl.on {
        print("The switch is on lets martch")
    }
}

I have a switch and a label in a custom cell in tableview. When I ON the switch i need to get the text in the label, can any one help how to make it possible.

Christian Abella
  • 5,747
  • 2
  • 30
  • 42
jerfin
  • 803
  • 1
  • 13
  • 28

5 Answers5

1

Use the tag property of UISwitch to store the position and use it in your handler to get the actual text form section array.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tbl_vw.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as!cuscellTableViewCell

    cell.txt_lbl.text = self.section[indexPath.row];

    cell.sel_btn.tag = indexPath.row

    cell.sel_btn.addTarget(self, action: #selector(self.switchChanged), forControlEvents: .ValueChanged)

    return cell

}



func switchChanged(sender: AnyObject) 
{
  let switchControl: UISwitch = sender as! UISwitch
  print("The switch is \(switchControl.on ? "ON" : "OFF")")

  let text = self.section[switchControl.tag]
  print(text)

  if switchControl.on 
  {
        print("The switch is on lets martch")
  }
} 

If you have multiple sections with multiple rows you can use a dictionary and store a tuple.

var items = [Int:(Int,Int)]()
...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tbl_vw.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as!cuscellTableViewCell

    cell.txt_lbl.text = self.section[indexPath.row];
    cell.sel_btn.tag = indexPath.row

    let tuple = (section: indexPath.section, row: indexPath.row)
    self.items[cell.sel_btn.hashValue] = tuple
    cell.sel_btn.addTarget(self, action: #selector(self.switchChanged), forControlEvents: .ValueChanged)

    return cell

}

func switchChanged(sender: AnyObject) 
    {
      let switchControl: UISwitch = sender as! UISwitch
      print("The switch is \(switchControl.on ? "ON" : "OFF")")

      let tuple = self.items[switchControl.hashValue]
      print(tuple.0)  // this is the section
      print(tuple.1) // this is the row

      if switchControl.on 
      {
            print("The switch is on lets martch")
      }
    } 
Christian Abella
  • 5,747
  • 2
  • 30
  • 42
  • In case of multiple sections in `UITableView`, It won't work. – Dipen Panchasara Aug 19 '16 at 05:20
  • Christina Abella how can i achieve the same functionality in case of Multiple Sections. – jerfin Aug 19 '16 at 05:39
  • @Arun Combine section and row by multiplying section * 100: `let tag = (indexPath.section + 1) * 100 + indexPath.row` and back `let indexPath = NSIndexPath(forRow: tag % 100, inSection: (tag / 100) - 1)` An extra array is not needed. – vadian Aug 19 '16 at 05:49
  • I updated my answer. If you want to use it in a table with multiple sections and rows, use the hashValue of the control as key and store the section in row in a tuple. – Christian Abella Aug 19 '16 at 05:49
0

In your case move func switchChanged(sender: AnyObject) to your custom class and assign selector in customCell for UISwitch. You will start receiving your switchChange event in your customCell now you are customCell you have access to your UISwitch & UILabel objects, also keep a boolean flag to maintain switch state if required.

  • Step 1: Move your switchChanged to customCell class.
  • Step 2: In customCell awakeFromNib or init method assign selector to UISwitch object to cell itself to receive switch change event in cell.
  • Step 3: Once your switch change event fires update your UILabel as you have access to it inside customCell.

Let me know if you have any doubt.

Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
0

Set tag of sel_btn(UISwitch) in cellForRowAtIndexPath method.

sel_btn.tag = indexPath.row

On switchChanged method, get the tag of sel_btn and thus text of label.

let switchControl: UISwitch = sender as! UISwitch
        let tag = switchControl.tag
        let cell : cuscellTableViewCell = tbl_vw.cellForRowAtIndexPath(NSIndexPath(forRow: tag, inSection: 0)) as? cuscellTableViewCell
        print(cell.txt_lbl.text)

If you want to get data from model not from view, then override the above two lines with below line:-

let textValue = self.section[tag];
pkc456
  • 8,350
  • 38
  • 53
  • 109
0

what you need to do is use block

from Get button click inside UI table view cell

first move your action method to table cell

in tableview cell add block property

 @property (copy, nonatomic) void (^didTapButtonBlock)(id sender);

with your action method

call block

- (void)didTapButton:(id)sender {
    if (self.didTapButtonBlock)
    {
        self.didTapButtonBlock(sender);
    }
}

and receive that from cell , you have both section and row there

     [cell setDidTapButtonBlock:^(id sender)
     {
         // Your code here

     }];

Hope it helps

Community
  • 1
  • 1
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
0
In tableview delegate method -

   tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)  

add index path value to switch tag value - 

cell.sel_btn.tag = indexPath.row


In Switch Value change method just get selected text when that switch is ON


func switchChanged(sender: AnyObject) 
{
  let switchControl: UISwitch = sender as! UISwitch
  print("The switch is \(switchControl.on ? "ON" : "OFF")")

  // If switch ON
  if switchControl.on 
  {
        print("The switch is ON”)

       // Get Selected Label Text
        let selectedText = self.section[switchControl.tag]
        print(selectedText)

  }
} 
Ashwini Chougale
  • 1,093
  • 10
  • 26
  • How to achieve this in table view with multiple section. – jerfin Aug 19 '16 at 09:03
  • Maintain boolean array to show switch state in cellForRowAtIndexPath like : cell.mySwitch.tag = indexPath.row if (selectedFilterArray[indexPath.row] == "NO") { cell.mySwitch.on = false } else if (selectedFilterArray[indexPath.row] == "YES") { cell.mySwitch.on = true } – Ashwini Chougale Aug 19 '16 at 12:17
  • In Value change Method : func switchChange(mySwitch: UISwitch) { print("tag value \(Int(mySwitch.tag))") if mySwitch.on { print("tag value \(Int(mySwitch.tag))") print("textName \(self.section[mySwitch.tag])") } if !(selectedFilterArray[mySwitch.tag] == "NO") { //If checked mySwitch.on = true } else { //If not checked mySwitch.on = false } } – Ashwini Chougale Aug 19 '16 at 12:17