请任何人帮助我如何访问一个带有标签或文本标签的特定单元格,我有一个表格视图,上面有一个按钮列表,这些按钮在单元格的缩略图上显示,当单元格被单击时,该单元格的背景也会变红。
当我再次点击该单元格时--当它的红色--背景变成白色--默认情况下--从上面的按钮中移除缩略图。
另外,当我点击按钮,照片应该消失,以及细胞本身的背景,它应该是白色的。这正是我所不能做的。有人能帮我吗,如何访问这个单元格来改变它的背景,当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!那样的索引路径变量
发布于 2015-01-20 08:32:26
首先,将以下一行添加到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)
}
}发布于 2015-01-20 07:41:17
您不应该试图在标记或文本内容上匹配,所有内容都应该基于行或索引路径,以便它与您的数据模型相关。
考虑为按钮数据存储创建一个自定义类,这样每个按钮就有一个带有一个条目的数组,并且该类保存了相关单元格、图像等的索引路径。然后,当单击一个按钮时,很容易更新您的主数据模型和单元格(如果它是可见的)。
发布于 2015-01-20 08:23:57
您可以这样在单击事件中获取单元格的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)
}
}https://stackoverflow.com/questions/28039939
复制相似问题