Stepper on tableview cell (swift)

时间秒杀一切 提交于 2019-12-04 11:47:13
Cyril

As mentioned by @A-Live, your component is being reused and so need to be updated.

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

And

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
}

Reset the value of stepper while loading your cell. you can reset the cell property values in cell's prepareForReuse method. add the following method in your ReviewTableViewCell class.

 override func prepareForReuse() 
 {
   super.prepareForReuse()

   countStepper.value = 0.0 
 }

In tableViewCell VC:

1 - add these field

var cellDelegate: cellProtocol?
var index: IndexPath?

2 - then add this in the delegate:

func onStepperClick(index: Int, sender: UIStepper)

3 - when you have dragged your stepper over as an action use this:

@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
}

In ViewController

1 - add these to the tableView function that has the cellAtRow variable.

cell.cellDelegate = self
cell.index = indexPath 

2 - Use this instead of your stepperButton function

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

}

Hope this works for you

NOTE: 1.MY Cell class is just normal..All changes are in viewcontroller class 2.I have taken stepper and over it added ibAddButton with same constraint as 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 empty int array [Int]()

var countArray = [Int]()

2.append countArray with all zeros with the number of data u want to populate in tableview

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

3.in cell for row at

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 functions

 @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()
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!