如何从表视图中的自定义单元格中的标签获取文本

时间:2022-03-09 08:41:39
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.

我在tableview的自定义单元格中有一个开关和一个标签。当我在开关上我需要获得标签中的文本时,任何人都可以帮助如何使它成为可能。

5 个解决方案

#1


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.

使用UISwitch的tag属性存储位置并在处理程序中使用它来获取实际的文本表单节数组。

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")
      }
    } 

#2


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.

在您的情况下,将func switchChanged(sender:AnyObject)移动到您的自定义类,并在customCell中为UISwitch分配选择器。您将在customCell中开始接收switchChange事件,现在您是customCell,您可以访问UISwitch和UILabel对象,如果需要,还可以保留一个布尔标志来维护开关状态。

  • Step 1: Move your switchChanged to customCell class.
  • 第1步:将switchChanged移动到customCell类。

  • Step 2: In customCell awakeFromNib or init method assign selector to UISwitch object to cell itself to receive switch change event in cell.
  • 步骤2:在customCell中,awakeFromNib或init方法将选择器分配给UISwitch对象到单元格本身以接收单元格中的交换机更改事件。

  • Step 3: Once your switch change event fires update your UILabel as you have access to it inside customCell.
  • 第3步:切换更改事件后,如果您在customCell中有权访问它,则会更新您的UILabel。

Let me know if you have any doubt.

如果您有任何疑问,请告诉我。

#3


0  

what you need to do is use block

你需要做的是使用块

from Get button click inside UI table view cell

从获取按钮单击UI表格视图单元格

first move your action method to table cell

首先将您的操作方法移动到表格单元格

in tableview cell add block property

在tableview单元格中添加块属性

 @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

希望能帮助到你

#4


0  

Set tag of sel_btn(UISwitch) in cellForRowAtIndexPath method.

在cellForRowAtIndexPath方法中设置sel_btn(UISwitch)的标记。

sel_btn.tag = indexPath.row

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

在switchChanged方法中,获取sel_btn的标记,从而获得标签文本。

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];

#5


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)

  }
} 

#1


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.

使用UISwitch的tag属性存储位置并在处理程序中使用它来获取实际的文本表单节数组。

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")
      }
    } 

#2


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.

在您的情况下,将func switchChanged(sender:AnyObject)移动到您的自定义类,并在customCell中为UISwitch分配选择器。您将在customCell中开始接收switchChange事件,现在您是customCell,您可以访问UISwitch和UILabel对象,如果需要,还可以保留一个布尔标志来维护开关状态。

  • Step 1: Move your switchChanged to customCell class.
  • 第1步:将switchChanged移动到customCell类。

  • Step 2: In customCell awakeFromNib or init method assign selector to UISwitch object to cell itself to receive switch change event in cell.
  • 步骤2:在customCell中,awakeFromNib或init方法将选择器分配给UISwitch对象到单元格本身以接收单元格中的交换机更改事件。

  • Step 3: Once your switch change event fires update your UILabel as you have access to it inside customCell.
  • 第3步:切换更改事件后,如果您在customCell中有权访问它,则会更新您的UILabel。

Let me know if you have any doubt.

如果您有任何疑问,请告诉我。

#3


0  

what you need to do is use block

你需要做的是使用块

from Get button click inside UI table view cell

从获取按钮单击UI表格视图单元格

first move your action method to table cell

首先将您的操作方法移动到表格单元格

in tableview cell add block property

在tableview单元格中添加块属性

 @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

希望能帮助到你

#4


0  

Set tag of sel_btn(UISwitch) in cellForRowAtIndexPath method.

在cellForRowAtIndexPath方法中设置sel_btn(UISwitch)的标记。

sel_btn.tag = indexPath.row

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

在switchChanged方法中,获取sel_btn的标记,从而获得标签文本。

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];

#5


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)

  }
}