繁体   English   中英

tableview 单元格上的步进器(swift)

[英]Stepper on tableview cell (swift)

我将步进器的插座和动作放入 tableview 单元格中,并使用协议委托将其连接到 tableview。 当我在第一行点击步进器时,步进器值在第一行正常出现,但它也出现在一些随机行中。 如何解决这个问题?

表视图单元格

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

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

 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
}

在视图控制器中

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 Cell 类是正常​​的..所有更改都在 viewcontroller 类中 2.我已经采用 stepper 并在它上面添加了 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.定义空int数组[Int]()

var countArray = [Int]()

2. 将 countArray 与您想要在 tableview 中填充的数据数量一起添加为零

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM