在Swift for iOS中开发,我创建了一组Ints,我标记为amount
。数组中的每个Int表示特定项目的总数(amount
中的第一项代表汽车总数,第二项总线,第三辆自行车等)。使用表格视图,我在表格视图单元格中放置了一个按钮和标签。在表格视图中,我希望第一个单元格的标签显示amount
中的第一个Int,第二个单元格的标签显示amount
中的第二个Int,第三个单元格& #39; s标签显示amount
中的第三个Int,等等。我希望它的功能如下,每次按下内部按钮我想要相应的标签来增加它的显示总数一个人。例如,如果在第三个单元格中按下按钮7次,我希望第三个单元格的标签显示Int 7.如果在第五个单元格中按下按钮2次,我想要第五个单元格&#39 ; s标签显示Int 2.最好的方法是什么才能实现这一目标?
@IBOutlet weak var flavorTable: UITableView!
//this is the array that I want to contain the Ints to display in each cell's label, 1st cell should display amount[0], 2nd cell amount[1], etc.
var amount = [Int]()
func bookieButton(_ sender: UIButton) {
//this is where code that I want to increment the cell's label by one every time it is pressed but I'm unsure how to do so
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorCellTableViewCell
return cell
}
答案 0 :(得分:1)
你想要达到的目的是这样做的:
我使用dictionary
键创建了Integer
,其值为[Int]
。为了保持每cell
button
每个key
的点按次数,此index
将是一个单元array
值,tap
将保留{{1}的总数计数。
在这种情况下,重要的一点是如何维护以前cell
点击的count
数据,因此我为每dictionary
+保留了cell index
+我保留了更多检查,我在数组中保存以前的tapped index
,如果用户再次点击之前点按的cell
,我从<{1}获取保存的点击对于特定的单元格索引键,并将其更新回并保存回字典。
检查代码以便更好地理解,运行和调试,您将理解。
控制器类 - :(工作代码)
dictionary
自定义单元格类 - :
import UIKit
class ViewController: UIViewController {
// CELL DATA ARRAY
var data = [0,0,0,0,0,0,0,0,0,0]
// DICTIONARY TO HOLD NUMBER OF TAPS PER INDEX as [Int] (having tapped index as unique key)
var countDictionary = [Int:[Int]]()
// Array to hold tapped cell index
var indexs = [Int]()
// Initial tap count
var numberOfTapsPerCell : Int = 0
// This is the array given to dictionary as value(it has tapped count per cell)
var totalTaps = [Int]()
//MARK-: VIEW DID LOAD
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// INITIALIZE total tap ARRAY WITH INDEX 0
totalTaps = Array(repeating: 0, count: 1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// TABLE VIEW METHODS
extension ViewController:UITableViewDelegate,UITableViewDataSource{
// NUMBER OF ROWS
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// number of sections
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
// Cell for row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// deque cell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.customLabel.text = "Total taps : \(String(data[indexPath.row]))"
// CALL BACK BUTTON
cell.readCount = { [weak self] cell in
// Get index path of tapped cell
let index = tableView.indexPath(for: cell)
// Variable that holds previously tapped counts fetched from dictionary
var previousCountIncrement = 0
/* This loop checks if user is tapping new cell or previously tapped cell. If he taps new cell we re-initialize numberOfTapsPerCell to 0 (else lock) ,else we fetch value from dictionary for particular row , and increment previous value by 1 and save back in dictionary(if block) */
for selectedIndex in (self?.indexs)!{
if selectedIndex == index?.row{
let previousIndexCount = self?.countDictionary[selectedIndex]
for numberSum in previousIndexCount!{
previousCountIncrement = numberSum
previousCountIncrement += 1
cell.customLabel.text = "Total taps : \(String(previousCountIncrement))"
self?.data[selectedIndex] = previousCountIncrement
self?.totalTaps[0] = previousCountIncrement
self?.countDictionary[selectedIndex] = self?.totalTaps
return
}
}else{
self?.numberOfTapsPerCell = 0
}
}
/* Here we are saving tapped cell index in array. This is the code block which is always called once for each cell */
if let row = index?.row{
self?.indexs.append(row)
self?.numberOfTapsPerCell += 1
self?.totalTaps[0] = (self?.numberOfTapsPerCell)!
self?.countDictionary[row] = self?.totalTaps
let dict = self?.countDictionary[row]
for value in dict!{
cell.customLabel.text = "Total taps : \(String(value))"
self?.data[row] = value
}
}
}
return cell
}
}
希望能帮到你,让我知道是否有任何问题。感谢
答案 1 :(得分:0)
Swift 4.2
var increamentOrDecreamentValue = 0
@IBOutlet weak var label: UILabel!
@IBAction func increaseButtonTapped(_ sender: Any) {
increamentOrDecreamentValue += 1;
self.label.text = "\(increamentOrDecreamentValue)"
}
@IBAction func discreaseButtonTapped(_ sender: Any) {
if(increamentOrDecreamentValue != 0){
increamentOrDecreamentValue -= 1;
}
self.label.text = "\(increamentOrDecreamentValue)"
}
答案 2 :(得分:-1)
应该是最简单的方法,在tableView:cellForRowAt:indexPath:方法中的每个按钮上添加标记。您可以将标记设置为与indexPath.row相同。
现在从bookieButton:方法,检查该发件人的标记值是什么。因此标记值与indexPath.row相同。现在使用计数标签中的更新值更新该行。
您可以使用以下代码更新信号行
let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
答案 3 :(得分:-1)
你应该写下面的代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let row = indexPath.row
if(row < self.amount.count)
{
self.amount[row] += 1
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .none)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorCellTableViewCell
let row = indexPath.row
if(row < self.amount.count)
{
cell.countText = "\(self.amount[row])"
}
return cell
}