当我开始滚动textField
时,我需要将最后一个单元格的nil
值设置为tableView
时遇到问题。当我向下滚动一点,就像向下1厘米并按button
时,即使屏幕上有2个单元格,我的tableView
textField
的数据也会重新加载。
所以基本上我只需要重用我的LAST单元格,这是我的数组的最后一个元素。而不要碰其他细胞。
代码cellForRow
:
let cell = tableView.dequeueReusableCell(withIdentifier: "STAgentSplitTableViewCell", for: indexPath) as! STAgentSplitTableViewCell
let splits = self.agentSplits[indexPath.row]
if splits.from != nil && splits.to != nil && splits.percent != nil {
if indexPath.row == agentSplits.endIndex-1 {
cell.startValueCell.text = String(describing: splits.from!)
cell.endValueCell.text = String(describing: splits.to!)
cell.percentCell.text = "\(String(describing: splits.percent!))"
}
else {
cell.startValueCell.text = "$"+String(describing: splits.from!.withCommas())
cell.endValueCell.text = "$"+String(describing: splits.to!.withCommas())
cell.percentCell.text = "\(String(describing: splits.percent!))%"
}
if self.agentSplits[indexPath.row].to! == -1 {
cell.endValueCell.text = "Infinity"
}
}
if indexPath.row == agentSplits.endIndex-1 {
if from > to {
to = from
cell.endValueCell.text = String(describing: splits.from!)
}
if percent > 100 {
percent = 100
agentSplits[indexPath.row].percent! = 100
cell.percentCell.text = "\(String(describing: splits.percent!))"
}
}
cell.selectButton.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
if splits.isSelected == true {
cell.selectButton.isSelected = true
}
else {
cell.selectButton.isSelected = false
}
if splits.isCustom == true {
cell.startValueCell.placeholder = "Custom"
cell.endValueCell.placeholder = "Custom"
cell.percentCell.placeholder = "%"
}
else {
cell.startValueCell.isUserInteractionEnabled = false
cell.endValueCell.isUserInteractionEnabled = false
cell.percentCell.isUserInteractionEnabled = false
}
cell.startValueCell.addTarget(self, action: #selector(fromTextFieldValueChanged(_:)), for: .editingChanged)
cell.endValueCell.addTarget(self, action: #selector(toTextFieldValueChanged(_:)), for: .editingChanged)
cell.percentCell.addTarget(self, action: #selector(percentTextFieldValueChanged(_:)), for: .editingChanged)
return cell
}