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")
}
}
我在tableview的自定义单元格中有一个开关和一个标签。当我打开开关时,我需要在标签中找到文本,任何人都可以帮助如何使它成为可能。
答案 0 :(得分:1)
使用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")
}
}
答案 1 :(得分:0)
在您的情况下,将func switchChanged(sender: AnyObject)
移至自定义类,并在customCell中为UISwitch
指定选择器。您将在customCell中开始接收switchChange事件,即使您是customCell,您也可以访问UISwitch
& UILabel
个对象,如果需要,还保留一个布尔标志来维护开关状态。
switchChanged
移至customCell类。awakeFromNib
或init
方法中,将选择器指定给UISwitch
对象到单元格本身,以便在单元格中接收切换更改事件。UILabel
,因为您可以在customCell中访问它。如果您有任何疑问,请告诉我。
答案 2 :(得分:0)
在tag
方法中设置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];
答案 3 :(得分:0)
你需要做的是使用块
来自Get button click inside UI table view cell
首先将您的操作方法移动到表格单元格
tableview单元格中的添加块属性
@property (copy, nonatomic) void (^didTapButtonBlock)(id sender);
使用您的操作方法
呼叫阻止
- (void)didTapButton:(id)sender {
if (self.didTapButtonBlock)
{
self.didTapButtonBlock(sender);
}
}
并从单元格接收,你有那里的部分和行
[cell setDidTapButtonBlock:^(id sender)
{
// Your code here
}];
希望有所帮助
答案 4 :(得分: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)
}
}