Increase/Decrease 一个值并在 TableViewCell 内的标签中显示结果 Swift Xcode
Increase/Decrease a value and display results in a Label inside a TableViewCell Swift Xcode
我有一个 ViewController,其中包含一个 TableView 和一个包含多个部分和行的 TableViewCell。
我在每行中有 2 个按钮 "plus" 和 "minus" 以及一个标签 "totalLabel"。
当用户按下 + 或 - 按钮时,如何获取标签中显示的每个特定行的值?
现在,当我 运行 应用程序并按下 + 或 - 按钮时,只有第 0 部分/第 0 行的 totalLabel 工作,而随机值只是在另一个 sections/rows 中出现和消失
我的 tableViewCell 代码:
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
}
和我增加或减少价值的功能:
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
}
我尝试了无数解决方案,但到目前为止 none 有效 - 感谢您的帮助!
所以你的问题是这段代码
cell.plusButton.tag = indexPath.section
cell.plusButton.tag = indexPath.row
一个标签只能存储一个值。因此,您正在用该行覆盖该部分。所以它会引起各种奇怪的事情。更好的解决方案是根据按钮本身确定您要定位的单元格。由于您知道单击了哪个按钮,因此可以将此按钮的位置转换为 table 视图上的一个点。然后指向特定的索引路径。
因此,使用您的示例代码,您可以执行如下操作:
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])"
}
}
}
不知道你为什么要return数数。我确信这只是部分实现。但是按钮应该负责整个操作,包括使用新计数更新标签。您通常不会 return 按钮按下的值。
因此更新了示例以使用当前计数更新标签。因为我看不到你的饮料对象,所以我假设饮料 class 有一个从 0 开始的计数参数。这样每个单独的饮料都有一个分配给它的计数。
我有一个 ViewController,其中包含一个 TableView 和一个包含多个部分和行的 TableViewCell。 我在每行中有 2 个按钮 "plus" 和 "minus" 以及一个标签 "totalLabel"。
当用户按下 + 或 - 按钮时,如何获取标签中显示的每个特定行的值?
现在,当我 运行 应用程序并按下 + 或 - 按钮时,只有第 0 部分/第 0 行的 totalLabel 工作,而随机值只是在另一个 sections/rows 中出现和消失
我的 tableViewCell 代码:
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
}
和我增加或减少价值的功能:
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
}
我尝试了无数解决方案,但到目前为止 none 有效 - 感谢您的帮助!
所以你的问题是这段代码
cell.plusButton.tag = indexPath.section
cell.plusButton.tag = indexPath.row
一个标签只能存储一个值。因此,您正在用该行覆盖该部分。所以它会引起各种奇怪的事情。更好的解决方案是根据按钮本身确定您要定位的单元格。由于您知道单击了哪个按钮,因此可以将此按钮的位置转换为 table 视图上的一个点。然后指向特定的索引路径。
因此,使用您的示例代码,您可以执行如下操作:
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])"
}
}
}
不知道你为什么要return数数。我确信这只是部分实现。但是按钮应该负责整个操作,包括使用新计数更新标签。您通常不会 return 按钮按下的值。
因此更新了示例以使用当前计数更新标签。因为我看不到你的饮料对象,所以我假设饮料 class 有一个从 0 开始的计数参数。这样每个单独的饮料都有一个分配给它的计数。