Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何单击按钮并更改tablewviewcell中的标签文本_Swift_Uitableview_Uibutton_Uilabel - Fatal编程技术网

Swift 如何单击按钮并更改tablewviewcell中的标签文本

Swift 如何单击按钮并更改tablewviewcell中的标签文本,swift,uitableview,uibutton,uilabel,Swift,Uitableview,Uibutton,Uilabel,我想在单击每个TableViewCell中的按钮时更改标签文本 我的意思是有2个(-,+)按钮和1个标签,当我点击按钮时,标签会增加或减少 我不知道 怎么做? 您所要做的就是在按钮上添加一个操作并完成您的工作 @IBAction func increase() { quantityLabel.text = "\(quantity)" // increase the quantity number and set it } 在得到任何帮助之前,你应该考虑发布你的代码。 你所要做的就是把一

我想在单击每个TableViewCell中的按钮时更改标签文本

我的意思是有2个(-,+)按钮和1个标签,当我点击按钮时,标签会增加或减少

我不知道 怎么做?

您所要做的就是在按钮上添加一个操作并完成您的工作

@IBAction func increase() {
    quantityLabel.text = "\(quantity)" // increase the quantity number and set it
}

在得到任何帮助之前,你应该考虑发布你的代码。

你所要做的就是把一个动作添加到你的按钮并完成你的工作。

@IBAction func increase() {
    quantityLabel.text = "\(quantity)" // increase the quantity number and set it
}

BTW.在得到任何帮助之前,你应该考虑发布你的代码。

你需要维护一个<代码> QualTyTrase以存储当前索引的每一个索引< /P>

var quantityArray:[Int] = [] //initialize quantity array
然后将初始数量值添加到
quantityArray
例如:如果列表数组计数为10,且所有项目的初始数量为1,则表示 向数组中添加初始值

for i in 0 ..< 10 {
   quantityArray.append(1)
}
将以下功能添加到
viewcontroller

func incrementBtnClicked(_ sender:UIButton){
    let increasedQty = quantityArray[sender.tag]+1
    self.quantityArray.replaceSubrange(sender.tag, with: increasedQty)
    self.tableView.reloadData
}

func decrementBtnClicked(_ sender:UIButton){
    let decreasedQty = quantityArray[sender.tag]-1
    self.quantityArray.replaceSubrange(sender.tag, with: decreasedQty)
    self.tableView.reloadData
}
如果这样使用,即使在滚动tableview之后,单元格中的数量也将从数量数组中填充


希望这将帮助您

您需要维护一个
quantityArray
来存储每个索引的当前数量

var quantityArray:[Int] = [] //initialize quantity array
然后将初始数量值添加到
quantityArray
例如:如果列表数组计数为10,且所有项目的初始数量为1,则表示 向数组中添加初始值

for i in 0 ..< 10 {
   quantityArray.append(1)
}
将以下功能添加到
viewcontroller

func incrementBtnClicked(_ sender:UIButton){
    let increasedQty = quantityArray[sender.tag]+1
    self.quantityArray.replaceSubrange(sender.tag, with: increasedQty)
    self.tableView.reloadData
}

func decrementBtnClicked(_ sender:UIButton){
    let decreasedQty = quantityArray[sender.tag]-1
    self.quantityArray.replaceSubrange(sender.tag, with: decreasedQty)
    self.tableView.reloadData
}
如果这样使用,即使在滚动tableview之后,单元格中的数量也将从数量数组中填充


希望这对您有所帮助

到目前为止您都做了哪些尝试?发布一些代码。你可以使用library valuestepper来实现这一点。到目前为止,你尝试了什么?发布一些代码。您可以使用库valuestepper来实现这一点,让increasedQty=quantityArray[indexPath.row]-1我在indexPath.row处出错,我用这种方式更改函数,并让azalt=Int(urunler.adet[sender.tag])-1 self.urunler.adet[sender.tag]=String(azalt)self.separatimtableview.reloadData()。thanks@GaneshManickam它在sender.tag上显示“expected Range found Int”时出错。您为按钮设置了标记值吗@Neck@GaneshManickam是的,正如你在这里提到的,我已经给它们贴上了标签。比如:“itemCell.itemIncrementButton.tag=indepath.section”,甚至尝试了“itemCell.itemIncrementButton.tag=indepath.row”让incrematedqty=quantityArray[indepath.row]-1我在indepath.row处出错,我用这种方式更改函数,让azalt=Int(urunler.adet[sender.tag])-1 self.urunler.adet[sender.tag]=String(azalt)self.separatimtableview.reloadData()。thanks@GaneshManickam它在sender.tag上显示“expected Range found Int”时出错。您为按钮设置了标记值吗@Neck@GaneshManickam是的,正如你在这里提到的,我已经给它们贴上了标签。例如:“itemCell.itemIncrementButton.tag=indepath.section”,甚至尝试了“itemCell.itemIncrementButton.tag=indepath.row”