【发布时间】:2016-04-24 16:12:42
【问题描述】:
我将步进器的出口和动作都放入 tableview 单元中,并使用协议委托将其连接到 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))"
}
}
视图控制器
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)
}
}
【问题讨论】:
-
出队返回的单元格可重用 CellWithIdentifier 在用户滚动表格视图时被重用,您需要相应地更新每个视图。
-
hi @A-Live 如何更新呢?
-
无论如何我刚刚在这里更新了我的代码,因为我忘了复制它,我应该把这个代码“stepperLabel.text =”x(Int(countStepper.value))”放在哪里?
标签: ios swift uitableview tableviewcell uistepper