我有一个tableview,它的自定义tableviewcell看起来像这样。。。
此处显示的Tableviewcell
现在使用委托设置了名为option
的复选框,并在viewcontroller中访问该复选框,如下所示。。。
func optionBtnTapped(cell: UpdatingListTableViewCell) {
if let indexPath = standardsProceduresView.tableview?.indexPath(for: cell) {
cell.optionBtn.isSelected = !cell.optionBtn.isSelected
if(cell.optionBtn.isSelected)
{
//VALUE SET SINCE CHECKBOX ENABLED
... ... ...
let image = UIImage(named: "checkbox.png") as UIImage?
cell.optionBtn.setImage(image, for: UIControl.State.normal)
}
else
{
let image = UIImage(named: "square.png") as UIImage?
cell.optionBtn.setImage(image, for: UIControl.State.normal)
}
}
}
类似地,还设置了完成
复选框和计划
复选框(如上图所示)。
CellForRowatIndexPath
类似于。。。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UpdatingListTableViewCell = self.standardsProceduresView.dequeueReusableCellWithIdentifier(identifier: "cellClass") as! UpdatingListTableViewCell
cell.selectionStyle = .none
cell.delegate = self
//Setting up values from an array into the label named 'Risk assessment done' in the tableviewcell
resolutionDic = resolutionDetailsArray[indexPath.row] as! NSMutableDictionary
cell.stdProcedLbl.text = "\(resolutionDic["Resolution"]!)"
return cell
}
现在的问题是,如果我选中第一个单元格的复选框,然后向下滚动,一些不同单元格的其他复选框也显示为选中。 “完成”和“计划”复选框也会发生同样的情况。 对于名为WhotExtField
的textfield(如图所示)也会发生同样的情况 还有。 如果我在第一个单元格输入内容并向下滚动,文本会出现在我没有输入文本的单元格中。。。
参考了其他类似问题的SO职位。 但找不到解决方案。。。
编辑:这是我的TableViewCell代码。。
class UpdatingListTableViewCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var optionBtn: RoundButton!
@IBOutlet weak var plannedBtn: RoundButton!
@IBOutlet weak var doneBtn: RoundButton!
@IBOutlet weak var whoTextField: UITextField!
@IBOutlet weak var stdProcedLbl: UILabel!
@IBOutlet weak var nameLbl: UILabel!
var delegate: updatingDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
plannedBtn.isEnabled = false
doneBtn.isEnabled = false
whoTextField.delegate = self
whoTextField.autocorrectionType = .no
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func optionBtnTapped(_ sender: RoundButton) {
if let _ = delegate {
delegate?.optionBtnTapped(cell: self)
}
}
@IBAction func plannedBtnTapped(_ sender: RoundButton) {
if let _ = delegate {
delegate?.plannedBtnTapped(cell: self)
}
}
@IBAction func doneBtnTapped(_ sender: RoundButton) {
if let _ = delegate {
delegate?.doneBtnTapped(cell: self)
}
}
}
您的单元格正在被重用,因此当选中一个复选框并且您以后没有重置该复选框时,它的UI状态将被重用到不同的索引路径。 您可以在单元格的PrepareForReuse
函数中重置复选框,或者确保在CellForRowatIndexPath
中分配正确的复选框状态。
如果要重用UITableViewCell
,则如果希望每次滚动时在单元格中设置正确的数据,则必须在CellForRow
方法中填充相应单元格的整个单元格数据,因为每次滚动到新单元格时都会调用该方法。
您需要在PrepareForReuse()
中重置单元格的OptionBTN.IsSelected=false
和OptionBTN.SetImage(nil,for:。normal)
属性,如下所示(在UitableViewCell
子类中):
override func prepareForReuse() {
super.prepareForReuse()
optionBtn.isSelected = false
optionBtn.setImage(nil, for: .normal)
}