表格视图单元格上的步进器 (swift)

Stepper on tableview cell (swift)

我将 stepper outlets 和 action 放入 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))"

    }
}

ViewController

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 class.

中添加以下方法
 override func prepareForReuse() 
 {
   super.prepareForReuse()

   countStepper.value = 0.0 
 }

如@A-Live所述,您的组件正在被重用,因此需要更新。

所以在你的视图控制器中:

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
    }
})
    cell.countStepper.value = XXX[indexPath.row].value; //Here you update your view
    cell.stepperLabel.text = "x \(Int(cell.countStepper.value))" //And here

func stepperButton(sender: ReviewTableViewCell) {
    if let indexPath = tableView.indexPathForCell(sender){
        print(indexPath)
        XXX[sender.tag].value = sender.counterStepper.value //Here you save your updated value
}

在 tableViewCell 中 VC:

1 - 添加这些字段

var cellDelegate: cellProtocol?
var index: IndexPath?

2 - 然后在委托中添加:

func onStepperClick(index: Int, sender: UIStepper)

3 - 当你把你的步进器拖过来作为一个动作使用这个:

@IBAction func cellStepper(_ sender: UIStepper) {
    cellDelegate?.onStepperClick(index: (index?.row)!, sender: sender)
    sender.maximumValue = 1  //for incrementing
    sender.minimumValue = -1 //for decrementing
    //this will make sense later
}

在ViewController

1 - 将这些添加到具有 cellAtRow 变量的 tableView 函数中。

cell.cellDelegate = self
cell.index = indexPath 

2 - 使用这个代替你的 stepperButton 函数

func onStepperClick(index: Int, sender: UIStepper) {
    print(index)

    if sender.value == 1.0{
        //positive side of stepper was pressed
    }else if sender.value == -1.0{
        //negative side of stepper was pressed
    }

    sender.value = 0  //resetting to zero so sender.value produce different values on plus and minus

}

希望这对你有用

注意: 1.MY 单元格 class 正常..所有更改都在 viewcontroller class 2.I 已采用步进器并在其上添加了 ibAddButton,其约束与 ibStepper

相同
class cell: UITableViewCell {

    @IBOutlet weak var ibAddButton: UIButton!
    @IBOutlet weak var ibStepper: UIStepper!
    @IBOutlet weak var ibCount: UILabel!
    @IBOutlet weak var ibLbl: UILabel!
}

1.define 空整数数组 [Int]()

var countArray = [Int]()

2.append countArray,全为零,其中包含您要在 tableview 中填充的数据数量

 for arr in self.responseArray{
        self.countArray.append(0)
   }

3.in

行的单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
 IndexPath) -> UITableViewCell {

         let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! cell
         let dict = responseArray[indexPath.row] as? NSDictionary ?? NSDictionary()
         cell.ibLbl.text = dict["name"] as? String ?? String()
         if countArray[indexPath.row] == 0{
             cell.ibAddButton.tag = indexPath.row
             cell.ibStepper.isHidden = true
             cell.ibAddButton.isHidden = false
             cell.ibCount.isHidden = true
             cell.ibAddButton.addTarget(self, action: #selector(addPressed(sender:)), for: .touchUpInside)
         }else{
             cell.ibAddButton.isHidden = true
             cell.ibStepper.isHidden = false
             cell.ibStepper.tag = indexPath.row
             cell.ibCount.isHidden = false
             cell.ibCount.text = "\(countArray[indexPath.row])"
             cell.ibStepper.addTarget(self, action: #selector(stepperValueChanged(sender:)), for: .valueChanged)}
    return cell
     }

4.objc 函数

 @objc func stepperValueChanged(sender : UIStepper){
     if sender.stepValue != 0{
         countArray[sender.tag] = Int(sender.value)
     }
     ibTableView.reloadData()
 }
 @objc func addPressed(sender : UIButton){

     countArray[sender.tag] = 1//countArray[sender.tag] + 1
     ibTableView.reloadData()
 }