如何从 Table 视图中的自定义单元格中的标签获取文本
How to get Text from a label in custom cell in Table View
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")
}
}
我在表格视图的自定义单元格中有一个开关和一个标签。当我打开开关时,我需要在标签中获取文本,任何人都可以帮助如何实现它。
在您的情况下,将 func switchChanged(sender: AnyObject)
移动到您的自定义 class 并在 customCell 中为 UISwitch
分配选择器。您将开始在您的 customCell 中接收您的 switchChange 事件,现在您是 customCell,您可以访问您的 UISwitch
和 UILabel
对象,如果需要,还保留一个布尔标志以保持开关状态。
- 第 1 步:将您的
switchChanged
移至 customCell class。
- 步骤2:在customCell
awakeFromNib
或init
方法中将选择器分配给UISwitch
对象到单元格本身以接收switch change事件细胞.
- 第 3 步:一旦您的 switch 更改事件触发更新您的
UILabel
,因为您可以在 customCell 中访问它。
如有任何疑问,请告诉我。
使用 UISwitch 的标签 属性 来存储位置并在您的处理程序中使用它来获取实际的文本表单部分数组。
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")
}
}
在 cellForRowAtIndexPath
方法中设置 sel_btn
(UISwitch) 的 tag
。
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];
你需要做的是使用块
来自 Get button click inside UI table view cell
首先将您的操作方法移动到 table 单元格
在table查看单元格添加块属性
@property (copy, nonatomic) void (^didTapButtonBlock)(id sender);
用你的操作方法
调用块
- (void)didTapButton:(id)sender {
if (self.didTapButtonBlock)
{
self.didTapButtonBlock(sender);
}
}
并从 cell 接收到它,那里有部分和行
[cell setDidTapButtonBlock:^(id sender)
{
// Your code here
}];
希望对您有所帮助
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)
}
}
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")
}
}
我在表格视图的自定义单元格中有一个开关和一个标签。当我打开开关时,我需要在标签中获取文本,任何人都可以帮助如何实现它。
在您的情况下,将 func switchChanged(sender: AnyObject)
移动到您的自定义 class 并在 customCell 中为 UISwitch
分配选择器。您将开始在您的 customCell 中接收您的 switchChange 事件,现在您是 customCell,您可以访问您的 UISwitch
和 UILabel
对象,如果需要,还保留一个布尔标志以保持开关状态。
- 第 1 步:将您的
switchChanged
移至 customCell class。 - 步骤2:在customCell
awakeFromNib
或init
方法中将选择器分配给UISwitch
对象到单元格本身以接收switch change事件细胞. - 第 3 步:一旦您的 switch 更改事件触发更新您的
UILabel
,因为您可以在 customCell 中访问它。
如有任何疑问,请告诉我。
使用 UISwitch 的标签 属性 来存储位置并在您的处理程序中使用它来获取实际的文本表单部分数组。
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")
}
}
在 cellForRowAtIndexPath
方法中设置 sel_btn
(UISwitch) 的 tag
。
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];
你需要做的是使用块
来自 Get button click inside UI table view cell
首先将您的操作方法移动到 table 单元格
在table查看单元格添加块属性
@property (copy, nonatomic) void (^didTapButtonBlock)(id sender);
用你的操作方法
调用块
- (void)didTapButton:(id)sender {
if (self.didTapButtonBlock)
{
self.didTapButtonBlock(sender);
}
}
并从 cell 接收到它,那里有部分和行
[cell setDidTapButtonBlock:^(id sender)
{
// Your code here
}];
希望对您有所帮助
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)
}
}