tableview单元格上的步进(swift)

时间:2016-01-19 09:05:16

标签: ios swift uitableview tableviewcell uistepper

我将stepper的两个出口和动作放入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)
    }
}

4 个答案:

答案 0 :(得分:2)

加载单元格时重置步进器的值。您可以在单元格的prepareForReuse方法中重置单元格属性值。在ReviewTableViewCell课程中添加以下方法。

 override func prepareForReuse() 
 {
   super.prepareForReuse()

   countStepper.value = 0.0 
 }

答案 1 :(得分:1)

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

答案 2 :(得分:0)

在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

}

希望这对您有用

答案 3 :(得分:0)

注意: 1.MY Cell类很正常..所有更改都在viewcontroller类中 2.我已经使用了stepper,并在它上面添加了与ibStepper具有相同约束的ibAddButton

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数组[Int]()

var countArray = [Int]()

2。将countArray附加为全零,并添加要在表格视图中填充的数据数量

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

在第3行的单元格中

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()
 }