Ios 如何在每次按下按钮时将标签增加一个?

Ios 如何在每次按下按钮时将标签增加一个?,ios,arrays,swift,label,increment,Ios,Arrays,Swift,Label,Increment,在Swift for iOS中开发时,我创建了一个整数数组,并将其标记为数量。数组中的每个整数表示特定项目的总数(金额中的第一个项目表示汽车总数,第二个表示公共汽车总数,第三个表示自行车总数,依此类推)。使用表视图,我在表视图单元格内放置了一个按钮和标签。在表视图中,我希望第一个单元格的标签在金额中显示第一个整数,第二个单元格的标签在金额中显示第二个整数,第三个单元格的标签在金额中显示第三个整数,等等。我希望它的功能如下:,每次触摸按钮的内部,我都想增加相应的标签,使其显示总量增加一倍。例如,如

在Swift for iOS中开发时,我创建了一个整数数组,并将其标记为
数量
。数组中的每个整数表示特定项目的总数(金额中的第一个项目表示汽车总数,第二个表示公共汽车总数,第三个表示自行车总数,依此类推)。使用表视图,我在表视图单元格内放置了一个按钮和标签。在表视图中,我希望第一个单元格的标签在
金额中显示第一个整数,第二个单元格的标签在
金额中显示第二个整数,第三个单元格的标签在
金额中显示第三个整数,等等。我希望它的功能如下:,每次触摸按钮的内部,我都想增加相应的标签,使其显示总量增加一倍。例如,如果在第三个单元格中点击按钮7次,我希望第三个单元格的标签显示Int 7。如果在第五个单元格中点击按钮2次,我希望第五个单元格的标签显示Int 2。实现这一目标的最佳方式是什么

@IBOutlet weak var flavorTable: UITableView!

//this is the array that I want to contain the Ints to display in each cell's label, 1st cell should display amount[0], 2nd cell amount[1], etc.
var amount = [Int]()


func bookieButton(_ sender: UIButton) {
    //this is where code that I want to increment the cell's label by one every time it is pressed but I'm unsure how to do so

 }


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


      return cell

}

最简单的方法应该是,在tableView:cellForRowAt:indexPath:method内的每个按钮上添加标记。您可以将标记设置为与indexPath.row相同

现在从bookieButton:method中,检查该发送者的标记值。因此,标记值与indexPath.row相同。现在使用计数标签中的更新值更新该行

您可以使用以下代码来更新单行

let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)

您应该编写以下代码

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    let row = indexPath.row
    if(row < self.amount.count)
    {
        self.amount[row] += 1
        tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .none)

    }

}

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

    let row = indexPath.row
    if(row < self.amount.count)
    {
        cell.countText = "\(self.amount[row])"

    }


    return cell
}
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
让row=indexPath.row
如果(行UITableViewCell
{
让cell=tableView.dequeueReusableCell(标识符为“cell”)作为!FlavorCellTableViewCell
让row=indexPath.row
如果(行
您试图实现的目标可以通过以下方式实现:

我创建了一个
字典
,其中
整数
键和值为
[Int]
。要保持每个
单元格的点击次数
每个
按钮
,此
将是一个单元格
索引
值,
数组
将保存总的
点击次数
计数

在这种情况下,重要的一点是如何维护上一个
单元格
点击的
计数
数据,因此我为每个
单元格索引维护了一个
字典
,我维护了更多的检查,在数组中保存上一个
点击的索引
,如果用户再次点击之前点击的
单元格
,我将从
字典
中点击特定单元格索引键,然后将其更新并保存回字典

检查代码以便更好地理解、运行和调试您将理解的代码

控制器类-:(工作代码)

自定义单元格类-:

import UIKit

class CustomCell: UITableViewCell {

    // Outlets

    @IBOutlet weak var customCount: UIButton!
    @IBOutlet weak var customLabel: UILabel!

    // Call back function


    var readCount : ((CustomCell)->())?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    // Cell button action

    @IBAction func countTaps(_ sender: UIButton) {
        if let count = readCount{
            count(self)
        }

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
希望对您有所帮助,如果有任何问题,请告诉我。谢谢

Swift 4.2

var increamentOrDecreamentValue = 0
@IBOutlet weak var label: UILabel!

@IBAction func increaseButtonTapped(_ sender: Any) {

    increamentOrDecreamentValue += 1;
    self.label.text = "\(increamentOrDecreamentValue)"
}

@IBAction func discreaseButtonTapped(_ sender: Any) {

    if(increamentOrDecreamentValue != 0){
        increamentOrDecreamentValue -= 1;
    }
    self.label.text = "\(increamentOrDecreamentValue)"
}

这不是代码编写服务。你应该展示你所做的尝试和你所面临的问题!!!请编辑您的问题并发布您的尝试。您应该花一些时间阅读网站部分以及苹果的Swift图书,因为不可能将按钮连接到重复的内容。OP需要对一个表视图单元格进行子类化,并将按钮连接到该单元格。他将设置标记一次,而不是重复时间。单元标识符应类似于@“单元%d”,indexath.row。是的,子类也是另一种方法,因为他需要创建customCellDelegate。我不知道你为什么在我的回答中说否定。此方法也是有效的。单元格标识符应类似于@“Cell%d”,indexath.row。因此,每一行将有单独的标识符,并将使用特定的标记创建一次。
var increamentOrDecreamentValue = 0
@IBOutlet weak var label: UILabel!

@IBAction func increaseButtonTapped(_ sender: Any) {

    increamentOrDecreamentValue += 1;
    self.label.text = "\(increamentOrDecreamentValue)"
}

@IBAction func discreaseButtonTapped(_ sender: Any) {

    if(increamentOrDecreamentValue != 0){
        increamentOrDecreamentValue -= 1;
    }
    self.label.text = "\(increamentOrDecreamentValue)"
}