Swift-在TableView单元格中使用步进器增加标签 [英] Swift - Increment Label with Stepper in TableView Cell

查看:203
本文介绍了Swift-在TableView单元格中使用步进器增加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个Swift初学者在这里.我只是想在每个TableView单元格中使用一个步进器,以在同一单元格中增加标签.

Another Swift beginner here. I simply want a Stepper in each of my TableView cells that increments a label in the same cell.

我已经找到了有关此主题的几个问题,但是它们包含其他元素,因此我无法提取基本概念.

I have found a couple of questions on this topic, but they include other elements and I haven't been able to extract the basic concept.

更改UITextField和同一单元格内的UILabel

在表格视图单元格上的步进器(交换器)

到目前为止,我已经为单元格类中的Label和Stepper连接了IBOutlets,并为Stepper连接了IBAction.

So far I have connected IBOutlets for my Label and Stepper, as well as an IBAction for my Stepper in my cell class.

class BuyStatsCell: UITableViewCell{

    //these are working fine

    @IBOutlet weak var category: UILabel!
    @IBOutlet weak var average: UILabel!
    @IBOutlet weak var price: UILabel!


    //Outlet for Label and Stepper - How do I make these work?

    @IBOutlet weak var purchaseAmount: UILabel!
    @IBOutlet weak var addSubtract: UIStepper!

    //Action for Stepper - And this?

    @IBAction func stepperAction(_ sender: UIStepper) {
        self.purchaseAmount.text = Int(sender.value).description

    }

}

我了解在cellForRowAt indexPath中重用单元格的概念

And I understand the concept of reusing the cell in the cellForRowAt indexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "BuyStatsTabCell", for: indexPath) as! BuyStatsCell

        cell.isUserInteractionEnabled = false

 //these are working 

        cell.category.text = categories[indexPath.row]
        cell.price.text = String(prices[indexPath.row])
        cell.average.text = String(averages[indexPath.row])

//but is there something I need to add here to keep the correct Stepper and Label for each class?

    return cell
}

已经问到的一个问题包括协议和ViewController中的另一个函数,像这样

One of the already asked questions includes a protocol and another function in the ViewController like this

protocol ReviewCellDelegate{
    func stepperButton(sender: ReviewTableViewCell)
}

func stepperButton(sender: ReviewTableViewCell) {
    if let indexPath = tableView.indexPathForCell(sender){
        print(indexPath)
    }
}

我不知道这是否是我应该尝试的方法.我正在寻找最简单的解决方案,但是我很难将各个部分放在一起.

I don't know if this is the approach I should be trying to take. I am looking for the simplest solution, but I am having trouble putting the pieces together.

感谢您的帮助. 谢谢.

Any help is appreciated. Thanks.

推荐答案

最简单的解决方案(简化):

Easiest solution (simplyfied):

  • 创建一个具有属性purchaseAmount的模型BuyStat(成为一个类至关重要).
    强烈建议您不要使用多个数组作为数据源

  • Create a model BuyStat with a property purchaseAmount (it's crucial to be a class).
    You are strongly discouraged from using multiple arrays as data source

class BuyStat {

    var purchaseAmount = 0.0

    init(purchaseAmount : Double) {
        self.purchaseAmount = purchaseAmount
    }
}

  • 在视图控制器中创建一个数据源数组

  • In the view controller create a data source array

    var stats = [BuyStat]()
    

  • viewDidLoad中创建一些实例并重新加载表格视图

  • In viewDidLoad create a few instances and reload the table view

    stats = [BuyStat(purchaseAmount: 12.0), BuyStat(purchaseAmount: 20.0)]
    tableView.reloadData()
    

  • 在自定义单元格中创建一个属性buyStat,以使用观察者保存当前数据源项,以在设置buyStat时更新步进器和标签

  • In the custom cell create a property buyStat to hold the current data source item with an observer to update stepper and label when buyStat is set

    class BuyStatsCell: UITableViewCell {
    
        @IBOutlet weak var purchaseAmount: UILabel!
        @IBOutlet weak var addSubtract: UIStepper!
    
        var buyStat : BuyStat! {
            didSet {
                addSubtract.value = buyStat.purchaseAmount
                purchaseAmount.text = String(buyStat.purchaseAmount)
            }
        }
    
        @IBAction func stepperAction(_ sender: UIStepper) {
            buyStat.purchaseAmount = sender.value
            self.purchaseAmount.text = String(sender.value)
        }
    }
    

  • cellForRowAtIndexPath中获取数据源项并将其传递到单元格

  • In cellForRowAtIndexPath get the data source item and pass it to the cell

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BuyStatsTabCell", for: indexPath) as! BuyStatsCell
        cell.buyStat = stats[indexPath.row]
        return cell
    }
    

  • 魔术是:当您点击步进器时,标签以及数据源阵列将被更新.因此,即使在滚动后,单元格也将始终获得实际数据.

    The magic is: When you are tapping the stepper the label as well as the data source array will be updated. So even after scrolling the cell will get always the actual data.

    通过这种方式,您不需要协议或回调闭包.唯一重要的是,模型是具有引用类型语义的类.

    With this way you don't need protocols or callback closures. It's only important that the model is a class to have reference type semantics.

    这篇关于Swift-在TableView单元格中使用步进器增加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆