【发布时间】:2015-09-14 05:46:01
【问题描述】:
我有一个 TablewView,它的原型 UITableViewCell 有自己的类,
自定义 UITableViewCell 类
import UIKit
class H_MissingPersonTableViewCell: UITableViewCell {
@IBOutlet weak var strName: UILabel!
@IBOutlet weak var imgPerson: UIImageView!
@IBOutlet weak var Date1: UILabel!
@IBOutlet weak var Date2: UILabel!
@IBOutlet weak var MainView: UIView!
@IBOutlet weak var btnGetUpdates: UIButton!
@IBOutlet weak var btnComment: UIButton!
@IBOutlet weak var btnReadMore: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
TableView 加载完美,数据源和委托工作正常。但是,当我单击 IndexPath.row = 0(零)处的 BUTTON,然后向下滚动时,我会看到 random(?) 或其他单元格也因单击第 0 行中的 BUTTON 而被突出显示...
这是我的 CellForRowAtIndexPath 代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println("called")
var cell : CustomCell
cell = tableView.dequeueReusableCellWithIdentifier("CustomCellID", forIndexPath: indexPath) as! CustomCell
cell.strName.text = self.names[indexPath.row]
cell.imgPerson.image = UIImage(named: "\(self.persons[indexPath.row])")!
cell.btnGetUpdates.layer.borderColor = UIColor.darkGrayColor().CGColor
cell.btnGetUpdates.layer.borderWidth = 1
cell.btnGetUpdates.layer.cornerRadius = 5.0
cell.btnComment.layer.borderColor = UIColor.darkGrayColor().CGColor
cell.btnComment.layer.borderWidth = 1
cell.btnComment.layer.cornerRadius = 5.0
cell.btnReadMore.layer.borderColor = UIColor.darkGrayColor().CGColor
cell.btnReadMore.layer.borderWidth = 1
cell.btnReadMore.layer.cornerRadius = 5.0
cell.dateMissingPersonPlace.text = self.MissingPeoplePlace[indexPath.row]
cell.dateMissingPersonSince.text = self.MissingPeopleSince[indexPath.row]
cell.btnGetUpdates.tag = indexPath.row
cell.btnGetUpdates.addTarget(self, action: "GetUpdatesButton:", forControlEvents: .TouchUpInside)
return cell
}
在我的 cell.btnGetUpdates 中,我执行了一个操作,正如您在 CellForIndex 代码的最后一部分中看到的那样,GetUpdatesButton:
这是代码:
func GetUpdatesButton(sender: UIButton){
println("Button Clicked \(sender.tag)")
var sen: UIButton = sender
var g : NSIndexPath = NSIndexPath(forRow: sen.tag, inSection: 0)
var t : CustomCell = self.myTableView.cellForRowAtIndexPath(g) as! CustomCell
t.btnGetUpdates.backgroundColor = UIColor.redColor()
}
问题是,不仅索引 0 中的按钮被突出显示,而且当我向下滚动 tableView 时,我还会看到来自其他索引/行的随机按钮被突出显示。
我哪里出错了,我怎么只能更新我点击的按钮……而不是其他按钮……
我有 20 个项目(20 行),当我单击第 0 行中的按钮时,第 3、6、9 行......也有突出显示的按钮。
第 0 行,点击按钮
【问题讨论】:
-
这是一个非常常见的问题——你会在 SO 上找到很多关于它的问题。滚动时会重复使用单元格,因此您不能将状态数据存储在单元格对象中。您必须将状态存储在其他数据模型中,并使用它在
cellForRowAtIndexPath中设置按钮状态 - 即,如果它应该关闭则将其关闭,如果它应该打开则将其打开。 -
您可以在此处查看我的答案 - stackoverflow.com/questions/27918584/… - 该答案设置了一个复选标记,但您可以使用与按钮处理程序相同的方法并设置按钮颜色。主要思想是使用 NSMutableIndexSet 来存储所选项目
-
您好,非常感谢!我会看看这个,让你知道一段时间:)
-
我无法将 button.tag 添加到 NSMutableIndexSet 数组中...
-
您好我的问题没有解决,感谢您的回答,它帮助我掌握了概念,我使用了一个 INT 数组并将 indexPath.row 保存在那里,然后在 CellForRowAtIndex 中进行比较
标签: ios swift uitableview cocoa-touch