for 循环检查 cell.textlabel 或 cell.tag
for loop to check on cell.textlabel or cell.tag
任何人都可以帮助我如何访问带有标签或文本标签的特定单元格,我有一个 table 视图,上面有一个按钮列表,这些按钮在一旦单元格被点击,单元格的背景也会变红。
当我再次单击该单元格时 - 当它为红色时 - 背景变为白色 - 默认 - 缩略图也从上面的按钮中删除。
此外,当我单击按钮时,其中的照片以及单元格本身的背景应该消失,它应该变白。这就是我做不到的.. 谁能帮我解决这个问题,如何在 cell.tag 或单元格时访问单元格以更改其背景。 texlabel.text 给出。
提前致谢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = myTable.dequeueReusableCellWithIdentifier("intrest") as UITableViewCell
cell.tag = indexPath.row + 1
cell.textLabel?.text = InterstArr[indexPath.row]
var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40))
img.image = UIImage(named:"\(InterstArr[indexPath.row]).png")
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.indentationLevel = 1
cell.indentationWidth = 45
cell.addSubview(img)
return cell
}
我是在@natuslaedo 的帮助下完成的...我只是在单击该行时存储了索引路径的值.. 然后在我的 if 语句中检查上面的 5 个按钮并取消选择它们,我获取存储的索引路径并访问单元格以更改其颜色...我像这样初始化索引路径变量 var firstIP:NSIndexPath!
您不应尝试匹配标签或文本内容,一切都应基于行或索引路径,以便它与您的数据模型相关联。
考虑为您的按钮数据存储创建自定义 class,这样您就有一个数组,每个按钮一个条目,class 保存关联单元格、图像等的索引路径。然后,当单击按钮时,可以轻松更新主数据模型和单元格(如果它可见)。
这样可以获取点击事件中cell的indexPath
,
func buttonClick(sender:UIButton)
{
let position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(position)
{
println(indexPath.row)
println(indexPath.section)
}
}
将单元格中的按钮与您的数据模型匹配的另一种方法是将单元格的 indexPath 与单元格一起存储。点击按钮时,您的目标方法(单元格上的方法)将被调用,您可以使用 indexPath 并使用委托调用将其传递给数据模型。
我会为您提供更多详细信息,但我不确定这是否是您要问的。
首先,将以下行添加到您的 didSelectRowAtIndexPath: 函数中:
yourTableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
您按钮的目标选择器如下所示:
func buttonPressed(sender: UIButton!) {
// Code to remove photo from your button
// ......
// To remove background effect of cell
let indexPaths:Array<NSIndexPath> = yourTableView.indexPathsForSelectedRows() as Array<NSIndexPath>
// If you have implemented your own class which inherits from UITableViewCell, use its name while type casting and explicit typing
for indexPath in indexPaths {
let cell:UITableViewCell = yourTableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
// REMOVE BACKGROUND EFFECT HERE (using cell)
}
}
任何人都可以帮助我如何访问带有标签或文本标签的特定单元格,我有一个 table 视图,上面有一个按钮列表,这些按钮在一旦单元格被点击,单元格的背景也会变红。
当我再次单击该单元格时 - 当它为红色时 - 背景变为白色 - 默认 - 缩略图也从上面的按钮中删除。
此外,当我单击按钮时,其中的照片以及单元格本身的背景应该消失,它应该变白。这就是我做不到的.. 谁能帮我解决这个问题,如何在 cell.tag 或单元格时访问单元格以更改其背景。 texlabel.text 给出。
提前致谢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = myTable.dequeueReusableCellWithIdentifier("intrest") as UITableViewCell
cell.tag = indexPath.row + 1
cell.textLabel?.text = InterstArr[indexPath.row]
var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40))
img.image = UIImage(named:"\(InterstArr[indexPath.row]).png")
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.indentationLevel = 1
cell.indentationWidth = 45
cell.addSubview(img)
return cell
}
我是在@natuslaedo 的帮助下完成的...我只是在单击该行时存储了索引路径的值.. 然后在我的 if 语句中检查上面的 5 个按钮并取消选择它们,我获取存储的索引路径并访问单元格以更改其颜色...我像这样初始化索引路径变量 var firstIP:NSIndexPath!
您不应尝试匹配标签或文本内容,一切都应基于行或索引路径,以便它与您的数据模型相关联。
考虑为您的按钮数据存储创建自定义 class,这样您就有一个数组,每个按钮一个条目,class 保存关联单元格、图像等的索引路径。然后,当单击按钮时,可以轻松更新主数据模型和单元格(如果它可见)。
这样可以获取点击事件中cell的indexPath
,
func buttonClick(sender:UIButton)
{
let position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(position)
{
println(indexPath.row)
println(indexPath.section)
}
}
将单元格中的按钮与您的数据模型匹配的另一种方法是将单元格的 indexPath 与单元格一起存储。点击按钮时,您的目标方法(单元格上的方法)将被调用,您可以使用 indexPath 并使用委托调用将其传递给数据模型。
我会为您提供更多详细信息,但我不确定这是否是您要问的。
首先,将以下行添加到您的 didSelectRowAtIndexPath: 函数中:
yourTableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
您按钮的目标选择器如下所示:
func buttonPressed(sender: UIButton!) {
// Code to remove photo from your button
// ......
// To remove background effect of cell
let indexPaths:Array<NSIndexPath> = yourTableView.indexPathsForSelectedRows() as Array<NSIndexPath>
// If you have implemented your own class which inherits from UITableViewCell, use its name while type casting and explicit typing
for indexPath in indexPaths {
let cell:UITableViewCell = yourTableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
// REMOVE BACKGROUND EFFECT HERE (using cell)
}
}