我将 stepper 的 outlets 和 action 放入 tableview 单元格中,并使用协议(protocol)委托(delegate)将其连接到 tableview。当我在第一行点击步进器时,步进器值正常出现在第一行,但它也出现在一些随机行中。如何解决这个问题?
TableViewCell
protocol ReviewCellDelegate{
func stepperButton(sender: ReviewTableViewCell)
}
class ReviewTableViewCell: UITableViewCell {
@IBOutlet weak var countStepper: UIStepper!
@IBOutlet weak var stepperLabel: UILabel!
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
}
@IBAction func stepperButtonTapped(sender: UIStepper) {
if delegate != nil {
delegate?.stepperButton(self)
stepperLabel.text = "x \(Int(countStepper.value))"
}
}
View Controller
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "reviewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ReviewTableViewCell
var imageView: UIImageView?
let photoG = self.photos[indexPath.row]
imageView = cell.contentView.viewWithTag(1) as? UIImageView
//let layout = cell.goodiesImage
let tag = indexPath.row // +1
cell.tag = tag
photoG.fetchImageWithSize(CGSize(width: 1000, height: 1000), completeBlock: { image, info in
if cell.tag == tag {
imageView?.image = image
cell.goodiesImage.image = image
}
})
func stepperButton(sender: ReviewTableViewCell) {
if let indexPath = tableView.indexPathForCell(sender){
print(indexPath)
}
}
最佳答案
在加载单元格时重置步进器的值。您可以在单元格的 prepareForReuse
方法中重置单元格属性值。在您的 ReviewTableViewCell
类中添加以下方法。
override func prepareForReuse()
{
super.prepareForReuse()
countStepper.value = 0.0
}
关于ios - 表格 View 单元格上的步进器(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34872540/