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")
}
}表视图中的自定义单元格中有一个开关和一个标签。当我打开开关时,我需要把文字放在标签上,谁能帮我把它变成可能呢?
发布于 2016-08-19 05:18:31
使用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")
}
} 如果有多个包含多行的节,则可以使用字典并存储元组。
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")
}
} 发布于 2016-08-19 05:15:06
在您的示例中,将func switchChanged(sender: AnyObject)移动到您的自定义类,并在customCell中为UISwitch分配选择器。您将开始在您的switchChange中接收您的customCell事件,现在您是customCell您可以访问您的UISwitch & UILabel对象,还可以保留一个布尔标志,以便在需要时保持开关状态。
switchChanged移动到customCell类。awakeFromNib或init方法中,将选择器分配给UISwitch对象到单元本身,以接收单元中的开关更改事件。UILabel。如果你有任何疑问请告诉我。
发布于 2016-08-19 05:18:47
将tag of sel_btn(UISwitch)设置为cellForRowAtIndexPath方法。
sel_btn.tag = indexPath.row在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)如果您想从模型中获得数据,而不是从视图中获取数据,那么用下面的行覆盖上述两行:-
let textValue = self.section[tag];https://stackoverflow.com/questions/39031468
复制相似问题