如何通过TableCellView“自定义"的switchButton获取rowIndex [英] How to get rowIndex by switchButton of TableCellView "Custom"

查看:30
本文介绍了如何通过TableCellView“自定义"的switchButton获取rowIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个带有自定义 TableViewCell 的简单 TableViewController.

I'm building a simple TableViewController, with a Custom TableViewCell.

在这个 TableViewCell 我有

In this TableViewCell I have

1) 图像视图2) 标签3) 开关按钮

1) ImageView 2) Label 3) Switch Button

例如,当我尝试启动我的应用程序 5 行时.如果用户单击其中一个行项目的切换按钮,我想启动一个方法.

Now for example I have when I try to start my application 5 row. I want to start a method if the user click on switch button of one of the rows items.

所以我建立了这个方法:

So I have build this method:

@IBAction func attivaLuci(sender: UISwitch) {
        if sender.on {
            //ATTIVO LA LUCE CORRISPONDENTE
        }else{
            //DISATTIVO LA LUCE CORRISPONDENTE
        }
    }

现在如何获取单元格的rowIndex?

now how can I get the rowIndex of the cell?

推荐答案

使用这个

 let row = sender.convert(sender.frame.origin, to: self.tableview)
 let indexPath = self.tableview.indexPathForRow(at: row)

或者干脆

switchbutton.tag = indexPath.row
@IBAction func attivaLuci(sender: UISwitch) 
{
  let rowindex = sender.tag      
}

这篇关于如何通过TableCellView“自定义"的switchButton获取rowIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆