我希望有一个表格视图单元格,其中包含一个按钮,每按一次按钮,该按钮会使标签增加1。因此,如果按下按钮,标签会显示:“1,2,3,4”等。我希望每个单元格行彼此独立,这意味着我只想要单元格行3中的增量按钮来修改单元格行3的标签和单元格行4的相同标签,依此类推。
但是,我目前的代码功能执行以下操作:
当我按下增量按钮时,它会更改所有单元格的标签,而不仅仅是按下按钮的标签(我不想这样)。
当我滚动表格视图时,即使我没有按下该行中的按钮,所有标签都会改变(我想也不想要这个,我只希望标签在相应的增量按钮时改变被压了。)
任何人都可以帮助我获取代码或帮助我构建一些代码来帮助我实现目标吗?
在此先感谢所有帮助,我们非常感谢!
我的代码:
import UIKit
var bookieFlavors = ["Chocolate Chip", "Sugar w/o icing", "Sugar w/ icing", "Peanut Butter", "Honey", "Shortbread", "Ginger", "Double Chocolate", "Macadamie Nut", "Oatmeal Raisin", "Snickerdoodle"]
var labelAmount = Int()
class FlavorsController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var flavorTable: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return bookieFlavors.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorTableCell
//flavor label configuration
cell.flavorLabel.text = bookieFlavors[indexPath.row]
//amount configuration
cell.bookieAmount.text = "= \(amount)"
return cell
class FlavorTableCell: UITableViewCell {
@IBOutlet weak var flavorLabel: UILabel!
@IBOutlet weak var bookieButton: UIButton!
@IBAction func bookieButton(_ sender: UIButton) {
for _ in 1...15 {
bookieAmount.text = "= \(String(describing: amount))"
}
labelAmount += 1
}
@IBOutlet weak var bookieAmount: UILabel!
答案 0 :(得分:1)
您无法在tableview单元格中添加按钮操作来执行此操作。请查看下面的解决方案代码。
import UIKit
class FlavorsController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var bookieFlavors = ["Chocolate Chip", "Sugar w/o icing", "Sugar w/ icing", "Peanut Butter", "Honey", "Shortbread", "Ginger", "Double Chocolate", "Macadamie Nut", "Oatmeal Raisin", "Snickerdoodle"]
var labelAmount = [Int]() // To keep track of the amount in each cell
@IBOutlet weak var flavorTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
for item in self.bookieFlavors {
self.labelAmount.append(0) //Initialise with default value
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return bookieFlavors.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorTableCell
//flavor label configuration
cell.flavorLabel.text = bookieFlavors[indexPath.row]
//amount configuration
cell.bookieAmount.text = "= \(self.labelAmount[indexPath.row])"
cell.bookieButton.tag = indexPath.row
cell.bookieButton.addTarget(self, action: #selector(bookieButton(_:)), for: .touchUpInside)
return cell
}
//To configure the button click and according changes
@IBAction func bookieButton(_ sender: UIButton) {
self.labelAmount[sender.tag] = self.labelAmount[sender.tag] + 1
let cell = self.flavorTable.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? FlavorTableCell
cell?.bookieAmount.text = "= \(self.labelAmount[sender.tag])"
}
}
class FlavorTableCell: UITableViewCell {
@IBOutlet weak var flavorLabel: UILabel!
@IBOutlet weak var bookieButton: UIButton!
@IBOutlet weak var bookieAmount: UILabel!
}