Uitableview 如何在Swift中使用自定义单元格中的UIButton更新UILabel?
我试图在自定义单元格中使用+和-Uitableview 如何在Swift中使用自定义单元格中的UIButton更新UILabel?,uitableview,swift,cells,Uitableview,Swift,Cells,我试图在自定义单元格中使用+和-ui按钮将计数器实现为ui标签。我已经设法使用addTarget和按钮的标记来引用行,但是我不确定如何更新标签,每次按下按钮时添加或减去1 我曾尝试直接从addDrink函数访问cell.drinkQtyLabel.text,但显然不起作用。我的研究指向使用代理,但我对Xcode不够精通,不知道如何处理这些信息 以下是单元格的屏幕截图: 这是自定义的单元格。swift: import UIKit class ProductListTableViewCell:
ui按钮将计数器实现为ui标签。我已经设法使用addTarget和按钮的标记来引用行,但是我不确定如何更新标签,每次按下按钮时添加或减去1
我曾尝试直接从addDrink
函数访问cell.drinkQtyLabel.text
,但显然不起作用。我的研究指向使用代理,但我对Xcode不够精通,不知道如何处理这些信息
以下是单元格的屏幕截图:
这是自定义的单元格。swift
:
import UIKit
class ProductListTableViewCell: UITableViewCell {
@IBOutlet var drinkNameLabel: UILabel! = UILabel()
@IBOutlet var drinkVesselLabel: UILabel! = UILabel()
@IBOutlet var drinkSizeLabel: UILabel! = UILabel()
@IBOutlet var drinkPriceLabel: UILabel! = UILabel()
@IBOutlet var drinkQtyLabel: UILabel! = UILabel()
@IBOutlet weak var subtractDrinkButton: UIButton! = UIButton()
@IBOutlet weak var addDrinkButton: UIButton! = UIButton()
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
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
}
}
以及cellforrowatinex
方法:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:ProductListTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ProductListTableViewCell
let drink:PFObject = self.productListData.objectAtIndex(indexPath.row) as PFObject
var Qty = 1
cell.drinkNameLabel.alpha = 0
cell.drinkVesselLabel.alpha = 0
cell.drinkSizeLabel.alpha = 0
cell.drinkPriceLabel.alpha = 0
cell.drinkQtyLabel.alpha = 0
cell.drinkNameLabel.text = drink.objectForKey("name") as? NSString
cell.drinkVesselLabel.text = drink.objectForKey("vessel") as? NSString
cell.drinkSizeLabel.text = drink.objectForKey("size") as? NSString
cell.drinkPriceLabel.text = drink.objectForKey("price") as? NSString
cell.drinkQtyLabel.text = String(Qty)
UIView.animateWithDuration(0.5, animations: {
cell.drinkNameLabel.alpha = 1
cell.drinkVesselLabel.alpha = 1
cell.drinkSizeLabel.alpha = 1
cell.drinkPriceLabel.alpha = 1
cell.drinkQtyLabel.alpha = 1
})
cell.addDrinkButton.tag = indexPath.row
cell.drinkQtyLabel.tag = indexPath.row
cell.addDrinkButton.addTarget(self, action: Selector("addDrink:"), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func addDrink(sender:UIButton){
println("Added drink in row \(sender.tag)"
//update cell.drinkQtyLabel.text
}
}
我想要的伪代码可能如下所示:
func addDrink(sender: UIButton){
for cell at indexPath(sender.tag){
cell.drinkQtyLabel.text = int(cell.drinkQtyLabel.text) + 1
}
}
但我知道这不是解决办法 我有点奇怪,访问标签文本并不会更新它。你确定你把它设置成了正确的值吗?我不确定你的意思。我正在尝试从addDrink函数中设置标签。我已经用一些伪代码更新了这个问题,我的大脑认为它应该如何工作!谢谢你看这个,我的意思是,你正在将它设置为一个特定的值。例如,触摸“+”后,值应为2,对吗?所以问题是,您能否验证您的代码是否尝试将“2”设置为标签。它不会。目前除了println之外,addDrink函数中没有其他代码。我所要做的就是检索被按下按钮的索引路径行。从addDrink函数中似乎看不到标签(这对于经验丰富的程序员来说可能是显而易见的)