【发布时间】:2018-08-10 02:52:36
【问题描述】:
我的 VC 中有一个表格视图。牢房内有一些标签和按钮。我在标签中传递了值,现在我正在尝试当我点击也在该单元格中的按钮时,它应该增加标签的值。该标签中的值来自以前的 VC。当按下按钮时,我已经为它指定了一个代表,当按下按钮时,它应该将标签的值增加其中存在的第一个价格。我正在尝试获取该单元格索引路径,但没有得到它。我的代码是这样的, 在我的 cel 类中,我有这个协议,
protocol cartDelegate {
func addTappedInCell(_ cell: CartTableViewCell)
func minusTappedInCell(_ cell: CartTableViewCell)
}
var delegate : cartDelegate?
@IBAction func addBtnTapped(_ sender: Any) {
delegate?.addTappedInCell(self)
}
@IBAction func minusBtnTapped(_ sender: Any) {
delegate?.minusTappedInCell(self)
}
在我的视图控制器中我正在尝试这个,
extension CartViewController : cartDelegate{
func addTappedInCell(_ cell: CartTableViewCell) {
guard let indexPath = cartTableView?.indexPath(for: cell) else { return }
print(indexPath)
total += 1
cell.totalLbl.text = String(total)
print(cell.priceLbl.text!)
count = "5"
let tPrice = cell.priceLbl.text! + count
print(tPrice)
cell.priceLbl.text = String(tPrice)
subTotalLbl.text = cell.priceLbl.text
}
func minusTappedInCell(_ cell: CartTableViewCell) {
total -= 1
cell.totalLbl.text = String(total)
price = Int(cell.priceLbl.text!)! - Int(count)!
cell.priceLbl.text = String(price)
subTotalLbl.text = cell.priceLbl.text
}
【问题讨论】:
-
看看this answer。
-
当您有单元格引用并正确更新单元格值时,您想对索引做什么?
-
我正在尝试在点击 + 按钮时更新价格值,并且它应该只增加点击哪个按钮的单元格价格。 @HarshalBhavsar
-
而上面的代码不起作用?
-
不,它不起作用。
标签: ios swift uitableview