Swift3 Swift 3 tableViewCell按钮,奇怪的行为,提交一个按钮和同一单元格的其他按钮更改?
重复了两个项目,搜索无效,所以我希望有人能帮助 创建了一个基本TableViewController和一个TableViewCell。运行并按下第一个按钮,向下滚动和其他随机按钮也显示相同的变化 TableViewControllerSwift3 Swift 3 tableViewCell按钮,奇怪的行为,提交一个按钮和同一单元格的其他按钮更改?,swift3,xcode8,Swift3,Xcode8,重复了两个项目,搜索无效,所以我希望有人能帮助 创建了一个基本TableViewController和一个TableViewCell。运行并按下第一个按钮,向下滚动和其他随机按钮也显示相同的变化 TableViewController import UIKit class TableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() } ove
import UIKit
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 99
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell", for: indexPath) as! ButtonCell
cell.rowLabel.text = "\(indexPath.row)"
cell.tapAction = { (cell) in
self.showAlertForRow(tableView.indexPath(for: cell)!.row)
}
return cell
}
// MARK: - Extracted method
func showAlertForRow(_ row: Int) {
let alert = UIAlertController(
title: "BEHOLD",
message: "Cell at row \(row) was tapped!",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Gotcha!", style: UIAlertActionStyle.default, handler: { (test) -> Void in
self.dismiss(animated: true, completion: nil)
}))
self.present(
alert,
animated: true,
completion: nil)
}
}
表视图单元
// ButtonCell.swift
import UIKit
class ButtonCell: UITableViewCell {
var tapAction: ((UITableViewCell) -> Void)?
@IBOutlet weak var rowLabel: UILabel!
@IBOutlet weak var button: UIButton!
@IBAction func buttonTap(_ sender: AnyObject) {
button.setTitle("Correct", for: .normal)
tapAction?(self)
}
}
发生这种情况是因为您正在将
UITableViewCells
从队列中取出。为了解决此问题,您应该在按钮cell
中使用prepareforeuse()
方法。您可以保存所有按钮的状态,并且当某个单元格正在排队时,您可以在prepareforeuse()
方法中设置该单元格中按钮的标题。让我们从基本内容开始。你不需要这个:
cell.tapAction = { (cell) in
self.showAlertForRow(tableView.indexPath(for: cell)!.row)
}
您最好使用apple框架:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
showAlertForRow(indexPath.row)
}
其次,在单元格中创建一个方法,如
import UIKit
class ButtonCell: UITableViewCell {
var tapAction: ((UITableViewCell) -> Void)?
@IBOutlet weak var rowLabel: UILabel!
@IBOutlet weak var button: UIButton!
@IBAction func buttonTap(_ sender: AnyObject) {
button.setTitle("Correct", for: .normal)
tapAction?(self)
}
func createCell() {
button.setTitle("Your button title", for: normal)
}
}
并在
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
cell.createCell()
...
}
希望我能提供帮助,但试着从这里的基础开始:我认为他不需要
createCell()
方法,因为他可以使用苹果提供的prepareforeuse()
方法。这是一种方法,但如果他想更改数据,他可以创建一个createCell(with Item:Item)
并直接将项目添加到单元格中。然后,您可以做任何您想做的事情,并且您的cellForRowAt
方法中没有完整的代码