- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有这个 tableView:
但我无法识别用户何时选择了该行中的 UISwitch。我知道我需要 UITableViewCell 类中的@IBAction,但我没有找到适合我的指南。我想在用户点击 UISwitch 时打印该行的索引路径。
这是我的单元格类:
class TicketsFilterCell: UITableViewCell {
@IBOutlet weak var textFilter: UILabel!
@IBOutlet weak var switchFilter: UISwitch!
class var reuseIdentifier: String? {
get {
return "TicketsFilterCell"
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我该怎么办?
提前致谢。
编辑
我的 cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: TicketsFilterCell? = tableView.dequeueReusableCellWithIdentifier("cellTicketFilter", forIndexPath: indexPath) as? TicketsFilterCell
cell?.delegate = self
if(indexPath.section == 0) {
let text = status[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 1) {
let text = queues [indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 2) {
let text = types[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
} else if(indexPath.section == 3) {
let text = severities[indexPath.row]?.capitalizedString
cell?.textFilter.text = text
}
return cell!
}
最佳答案
次元,
这是你可以做的:)
在您的单元格类中声明一个协议(protocol),我们将其称为 CellProtocol。
protocol CellProtocol : class {
func switchButtonTapped(WithStatus status : Bool, ForCell myCell : TicketsFilterCell)
}
在您的 TicketsFilterCell 类中声明一个变量来保存委托(delegate),
weak var delegate : CellProtocol!
当用户点击你的开关时触发代理
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.delegate .switchButtonTapped(WithStatus: selected, ForCell: self)
}
在您的 TableViewController 中确认协议(protocol)使用
class ViewController: UITableViewController,CellProtocol {
现在在您的 ViewController 的 cellForRoAtIndexPath 中,为每个单元格将委托(delegate)设置为 self。
cell.delegate = self
最后实现委托(delegate)方法如下
func switchButtonTapped(WithStatus status: Bool, ForCell cell: TicketsFilterCell) {
let indexPath = self.tableView .indexPathForCell(cell)
print("cell at indexpath \(indexPath) tapped with switch status \(status)")
}
编辑:
对于每个单元格,您已将 IBOutlet 拖到开关 :) 现在您只需要从 UISwitch 到单元格的 IBAction :)
@IBAction func switchTapped(sender: UISwitch) {
self.delegate.switchButtonTapped(WithStatus: sender.on, ForCell: self)
}
就是这样:)
关于ios - UISwitch - UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37723672/
案例:我有 2 个 UISwitch - Switch1 和 Switch2。Switch1 控制场景中 UILabel 的可见性。Switch2 激活后关闭 Switch1 及其可见性。 问题:激活
我基本上有两个切换按钮,我希望用户从两个播放器或组播放中进行选择。但是,我不希望用户能够在用户单击其中一个时理想地选择这两个,而另一个则关闭。如何最好地实现这一点? -(void)stateSwi
我正在开发一个应用程序,其中有一个 UISwitch(switch1) 控制其他 UISwitches(开关 2-5)。如果开关 1 打开,则开关 2-5 关闭并禁用。如果 switch1 关闭,sw
我在同一 View 上有多个 UISwitch。在我的表单上,我需要使用 UISwitch 像单选按钮(网络)。不能同时打开超过一个 UISwitch,我只需要一个。 如果一个 UISwitch 打开
我有 11 行或更多行。只需要在第一个单元格中创建一个 UISwitch。当我单击任何行时,UIswitch 会重复。 UITableViewCell *cell = [tableView dequ
我在 PFTableView 中使用 UISwitch。当我打开一个 UISwitch 时,它会触发不同单元格中的另一个开关。下面是 PFTableViewCell 的代码。 override fun
我有一个 UITableView,其中我以编程方式为某些单元格添加了一个 UISwitch 作为 accessoryView。对于委托(delegate)方法didSelectRowAtIndexPa
如何获取给定 iOS 的默认 UISwitch 缩略图图像/阴影等?我正在构建自己的自定义 UISwitch,并希望我创建的拇指 ImageView 看起来与默认 UISwitch 相同。 最佳答案
我有两个 UISwitch....当第二个打开时我需要关闭第一个当第二个关闭时也打开第一个...... 同样,当第一个打开时,第二个将关闭,副varca... 有什么想法吗? 最佳答案 为两个开关添加
是否可以将图像添加到 UISwitch 背景,例如当状态为 ON 时(作为一个背景)和当状态为 OFF 时(另一个背景图像)? 最佳答案 要更改背景颜色(不是图像),您只需执行以下操作即可。这会更改领
我需要实现一个 View ,我需要用户使用"is"或“否”回答几个问题。我想过使用 UISwitch,但据我所知,它不允许用 YES/NO 替换它显示的 ON/OFF 消息。我错了吗? 我想过实现一个
我可以使用下面的代码更改 UISwitch 的颜色,但颜色始终为黄色,如何将颜色更改为另一种颜色? UISwitch *switch1=[[UISwitch alloc]initWithFrame:C
我有一个 UISwitch我把它放在我的 Storyboard 中并将它连接到我的实现文件中的 socket ,并连接了valueChanged:控制事件“值已更改”的方法。但是,仅当我将其从关闭切换
我想让 UISwitch 周围的触摸区域每边大 10 个点。查看相关帖子 (UIButton: Making the hit area larger than the default hit area
我正在开发一个具有 UISwitch 的应用程序。我想以编程方式将开关设置为开/关。我是这样试的,但是当setting时开关已经打开了是 false ... 有任何想法吗? @IBOutlet var
当用户打开开关时,我希望在 UILabel 上显示一个随机值,然后采用相同的随机值,并用它相乘并找到另一个值。我认为我的问题是我尝试使用 let 语句来保存该值,然后尝试从另一个函数回调该 let 值
我的 View Controller 上有一个 UISwitch,所以当我切换它时,按钮的文本会发生变化。我第一次将其关闭和打开时不起作用,但是如果您第二次尝试它,它就会起作用...我的代码中是否缺少
这可能看起来很傻,也许我理解不正确。 我的场景中有一个 UISwitch。 when a login button is clicked if the switch is on - do
我在我的应用程序中使用了 UISwitch 和 valueChanged 目标,如下代码所示。这个开关在我的 UITableView 中 settingsTableCell.androidSwitch
首先是我的 .h 文件: @interface SettingsViewController : UIViewController { IBOutlet UISwitch *gravaSwi
我是一名优秀的程序员,十分优秀!