问题描述
我正在尝试从另一个 tableviewcell 中按下的按钮更新 tableview。 这是我的带有表格视图的 VC 的代码:
import UIKit
class ResultsVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var teamTable: UITableView!
@IBOutlet weak var wordsTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
wordsForTheRound.removeLast()
print(results)
teamTable.delegate = self
teamTable.dataSource = self
wordsTable.delegate = self
wordsTable.dataSource = self
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
if tableView == self.teamTable {return results.count}
if tableView == self.wordsTable {return results[0].words.count}
return 0
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.teamTable {
if let cell = tableView.dequeueReusableCell(withIdentifier: "ResultsTeamsCell") as? ResultsTeamsCell {
cell.updateCell(result: results[indexPath.row])
return cell
}
}
if tableView == self.wordsTable {
if let cell = tableView.dequeueReusableCell(withIdentifier: "ResultsWordsCell") as? ResultsWordsCell {
cell.updateCell(wordIncmoing: results[0].words[indexPath.row],tf: results[0].wordisCorrect[indexPath.row])
return cell
}
}
return UITableViewCell()
}
@IBAction func reloadTables(_ sender: Any) {
teamTable.reloadData()
wordsTable.reloadData()
}
}
以及我需要teamTable重新加载的tableviewcell代码,同时我点击按钮TFBtnpressed
import UIKit
class ResultsWordsCell: UITableViewCell {
@IBOutlet weak var word: UILabel!
@IBOutlet weak var TFBtn: UIButton!
func updateCell(wordIncmoing: String,tf: Bool) {
word.text = wordIncmoing
TFBtn.setTitle("\(tf)",for: .normal)
}
@IBAction func TFBtnpressed(_ sender: Any) {
if TFBtn.title(for: .normal) == "false" {
TFBtn.setTitle("true",for: .normal)
changeWordStatus(word: word.text!)
results[0].score += 1
print(results)
} else {
TFBtn.setTitle("false",for: .normal)
changeWordStatus(word: word.text!)
results[0].score -= 1
print(results)
}
}
func changeWordStatus(word: String) {
for i in 0...results[0].words.count - 1 {
if word == results[0].words[i] {
results[0].wordisCorrect[i] = !results[0].wordisCorrect[i]
}
}
}
}
我阅读了很多文章,可能应该设置一个tableview委托,但我不知道该怎么做。
解决方法
您可以创建一个协议来在您的视图控制器和表格视图单元格之间进行通信。
在“ResultsWordsCell”中创建一个新协议;
import UIKit
protocol ResultsWordsCellDelegate {
func didTapRefreshButton()
}
class ResultsWordsCell: UITableViewCell {
.....
var resultsWordsCellDelegate: ResultsWordsCellDelegate?
.....
@IBAction func TFBtnPressed(_ sender: Any) {
.....
resultsWordsCellDelegate?.didTapRefreshButton()
.....
}
在 ResultsVC 的 cellForRowAt 函数上;
添加 -> cell.resultsWordsCellDelegate = self
现在,在 ResultsVC 上创建一个新的扩展;
class ResultsVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
.....
.....
}
extension ResultsVC: ResultsWordsCellDelegate {
func didTapRefreshButton {
self.wordsTable.reloadData()
self.teamTable.reloadData()
}
}
,
你可以使用clouser。 首先像这样为回调创建类型化:
typealias CallBack = () -> Void
并在 tableViewCell 中定义 clouser:
@IBOutlet weak var word: UILabel!
@IBOutlet weak var TFBtn: UIButton!
public var callBack: CallBack?
并在 TFBtnPressed 中调用变量:
@IBAction func TFBtnPressed(_ sender: Any) {
if TFBtn.title(for: .normal) == "false" {
TFBtn.setTitle("true",for: .normal)
changeWordStatus(word: word.text!)
results[0].score += 1
print(results)
} else {
TFBtn.setTitle("false",for: .normal)
changeWordStatus(word: word.text!)
results[0].score -= 1
print(results)
}
// set callBack
callBack?()
}
并在出队单元内更改此:
cell.updateCell(wordIncmoing: results[0].words[indexPath.row],tf: results[0].wordIsCorrect[indexPath.row])
cell.callBack = { [weak self] in
guard let self = self else {
return
}
// reload table here
self.wordsTable.reloadData()
self.teamTable.reloadData()
}
所有代码ResultWordsCell:
typealias CallBack = () -> Void
class ResultsWordsCell: UITableViewCell {
@IBOutlet weak var word: UILabel!
@IBOutlet weak var TFBtn: UIButton!
public var callBack: CallBack?
func updateCell(wordIncmoing: String,tf: Bool) {
word.text = wordIncmoing
TFBtn.setTitle("\(tf)",for: .normal)
}
@IBAction func TFBtnPressed(_ sender: Any) {
if TFBtn.title(for: .normal) == "false" {
TFBtn.setTitle("true",for: .normal)
changeWordStatus(word: word.text!)
results[0].score -= 1
print(results)
}
// set callBack
callBack?()
}
func changeWordStatus(word: String) {
for i in 0...results[0].words.count - 1 {
if word == results[0].words[i] {
results[0].wordIsCorrect[i] = !results[0].wordIsCorrect[i]
}
}
}
}