Ios 增加/减少值并在TableViewCell Swift Xcode内的标签中显示结果
我有一个带有TableView的ViewController和一个包含多个节和行的TableViewCell。 我有两个按钮“加号”和“减号”,每行有一个标签“TotalAbel” 当用户按下+或-按钮时,如何获取标签中显示的每个特定行的值 现在,当我运行应用程序并按下+或-按钮时,只有部分0/行0的TotalAbel工作,而随机值在其他部分/行中出现和消失 我的tableViewCell代码:Ios 增加/减少值并在TableViewCell Swift Xcode内的标签中显示结果,ios,swift,button,tableview,tableviewcell,Ios,Swift,Button,Tableview,Tableviewcell,我有一个带有TableView的ViewController和一个包含多个节和行的TableViewCell。 我有两个按钮“加号”和“减号”,每行有一个标签“TotalAbel” 当用户按下+或-按钮时,如何获取标签中显示的每个特定行的值 现在,当我运行应用程序并按下+或-按钮时,只有部分0/行0的TotalAbel工作,而随机值在其他部分/行中出现和消失 我的tableViewCell代码: import UIKit protocol CommandeCellDelegate: class
import UIKit
protocol CommandeCellDelegate: class {
}
class CommandeCell: UITableViewCell {
weak var delegate : CommandeCellDelegate!
@IBOutlet weak var drinksLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var totalLabel: UILabel!
@IBOutlet weak var plusButton: UIButton!
@IBOutlet weak var minusButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
以下是我的cellForRowAt代码:
class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CommandeCellDelegate {
var count : Int = 0
var countValue : String!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CommandeCell", for: indexPath) as! CommandeCell
cell.plusButton.tag = indexPath.section
cell.plusButton.tag = indexPath.row
cell.plusButton.addTarget(self, action: #selector(self.increaseValue), for: .touchUpInside)
cell.minusButton.tag = indexPath.section
cell.minusButton.tag = indexPath.row
cell.minusButton.addTarget(self, action: #selector(self.decreaseValue), for: .touchUpInside)
if indexPath.section == 0 {
let softInfo = softs[indexPath.row]
cell.drinksLabel?.text = softInfo.drinkName
cell.totalLabel?.text = // how to display countValue here?
let HappyHourStatus = partner!.barHHStatus
if case "0" = HappyHourStatus {
cell.priceLabel?.text = softInfo.drinkHHPrice
} else
if case "1" = HappyHourStatus {
cell.priceLabel?.text = softInfo.drinkPrice
}
}
else if indexPath.section == 1 {
let cocktailInfo = cocktails[indexPath.row]
cell.drinksLabel?.text = cocktailInfo.drinkName
cell.totalLabel?.text = // how to display countValue here?
let HappyHourStatus = partner!.barHHStatus
if case "0" = HappyHourStatus {
cell.priceLabel?.text = cocktailInfo.drinkHHPrice
} else
if case "1" = HappyHourStatus {
cell.priceLabel?.text = cocktailInfo.drinkPrice
}
}
return cell
}
和my funcs来增加或减少该值:
func increaseValue(_ sender: UIButton) -> Int {
count = 1 + count
print(count)
countValue = "\(count)"
let rowToReload = IndexPath(row: sender.tag, section: sender.tag)
let rowsToReload: [Any] = [rowToReload]
tableView.reloadRows(at: rowsToReload as! [IndexPath], with: .automatic)
return count
}
func decreaseValue(_ sender: UIButton) -> Int {
if count == 0 {
print("Count zero")
} else {
count = count - 1
}
countValue = "\(count)"
let rowToReload = IndexPath(row: sender.tag, section: sender.tag)
let rowsToReload: [Any] = [rowToReload]
tableView.reloadRows(at: rowsToReload as! [IndexPath], with: .automatic)
return count
}
我已经尝试了无数的解决方案,但到目前为止没有一个是有效的-谢谢你的帮助 所以你的问题是这个代码
cell.plusButton.tag = indexPath.section
cell.plusButton.tag = indexPath.row
标记只能存储一个值。因此,您正在使用该行覆盖该节。所以这会引起各种各样的奇怪。更好的解决方案是根据按钮本身确定目标细胞。因为您知道单击了什么按钮,所以可以将此按钮的位置转换为表视图上的一个点。然后指向一个特定的索引路径
因此,使用示例代码,您可以执行以下操作:
var softsCount: [Int] = []
var cocktailsCount: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
softsCount = Array(repeating: 0, count: softs.count) // Fill an array with 0
cocktailsCount = Array(repeating: 0, count: cocktails.count) // Fill an array with 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
if indexPath.section == 0 {
...
cell.totalLabel?.text = "\(softsCount[indexPath.row])"
...
} else if indexPath.section == 1 {
...
cell.totalLabel?.text = "\(cocktailsCount[indexPath.row])"
...
}
...
}
func increaseValue(_ sender: UIButton) {
let pointInTable = sender.convert(sender.bounds.origin, to: tableView)
if let indexPath = self.tableView.indexPathForRow(at: pointInTable), let cell = tableView.cellForRow(at: indexPath) {
if indexPath.section == 0 {
softsCount[indexPath.row] += 1
cell.totalLabel?.text = "\(softsCount[indexPath.row])"
} else if indexPath.section == 1 {
cocktailsCount[indexPath.row] += 1
cell.totalLabel?.text = "\(cocktailsCount[indexPath.row])"
}
}
}
不知道你为什么要返回计数。我相信这只是部分实施。但是按钮应该负责整个操作,包括用新计数更新标签。您通常不会从按下按钮返回值
因此,更新了示例以使用当前计数更新标签。因为我看不到你的饮料对象是什么,所以我假设饮料类有一个从0开始的count参数。这样,每种饮料都有一个计数。当前文本字段?我想可能是button?抱歉,这是我从当前项目复制和粘贴代码时得到的结果。更新为使用正确的变量名。非常感谢您的回答,我更改了递增/递减值func,但现在所有行都显示该值。如何让TotalAbel仅返回点击按钮所在行的值?因此需要更新数据源。使其具有正确的值。然后您可能应该使用您的数据源来计算新的总数。而不是依赖计数变量。我也只更新标签,而不是整行。让我更新我的代码,让你有一个更好的想法。因此,更新了示例,用当前计数更新标签。因为我看不到你的饮料对象是什么,所以我假设饮料类有一个从0开始的count参数。这样,每种饮料都有一个计数。