首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在表视图中自定义单元格中从标签中获取文本

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

Stack Overflow用户
提问于 2016-08-19 05:06:26
回答 5查看 1.7K关注 0票数 1
代码语言:javascript
复制
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")
    }
}

表视图中的自定义单元格中有一个开关和一个标签。当我打开开关时,我需要把文字放在标签上,谁能帮我把它变成可能呢?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-08-19 05:18:31

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

代码语言:javascript
复制
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")
  }
} 

如果有多个包含多行的节,则可以使用字典并存储元组。

代码语言:javascript
复制
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")
      }
    } 
票数 1
EN

Stack Overflow用户

发布于 2016-08-19 05:15:06

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

  • 步骤1:将switchChanged移动到customCell类。
  • 步骤2:在customCell、awakeFromNibinit方法中,将选择器分配给UISwitch对象到单元本身,以接收单元中的开关更改事件。
  • 步骤3:一旦您的开关更改事件触发,就可以在customCell中更新您的UILabel

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

票数 0
EN

Stack Overflow用户

发布于 2016-08-19 05:18:47

tag of sel_btn(UISwitch)设置为cellForRowAtIndexPath方法。

代码语言:javascript
复制
sel_btn.tag = indexPath.row

switchChanged方法中,获取sel_btn标记和标签文本。

代码语言:javascript
复制
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)

如果您想从模型中获得数据,而不是从视图中获取数据,那么用下面的行覆盖上述两行:-

代码语言:javascript
复制
let textValue = self.section[tag];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39031468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档