[英]Reload UITableView when button clicked in custom UITableViewCell
I have a button in my table view cell and I want to reload the whole view, which is a base controller. 我的表格视图单元格中有一个按钮,我想重新加载整个视图,这是基本控制器。
This class is what I want to reload (refresh, recall the view controller maybe). 此类是我想要重新加载的类(刷新,也许重新调用视图控制器)。
import UIKit
class TableVC: BaseController, DBDelegate, PLDelegate {
@IBOutlet weak var tableViewDB: UITableView!
}
And this is where I must do it: 这是我必须执行的操作:
import UIKit
class DailySpeakingLesson: UITableViewCell {
}
Use a delegate for this. 为此使用委托 。
Set the delegate for the custom cell in tableView(_:cellForRowAt:)
, then call tableViewDB.reloadData()
inside the delegate's function. 在
tableView(_:cellForRowAt:)
设置自定义单元的委托,然后在委托的函数内调用tableViewDB.reloadData()
。
TableVC 桌面VC
class TableVC: BaseController, DBDelegate, PLDelegate, DailySpeakingLessonDelegate {
@IBOutlet weak var tableViewDB: UITableView!
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dailySpeakingLesson = tableView.dequeueReusableCell(withIdentifier: "cellId") as! DailySpeakingLesson
dailySpeakingLesson.delegate = self
return dailySpeakingLesson
}
func dailySpeakingLessonButtonPressed() {
tableViewDB.reloadData()
}
}
DailySpeakingLesson 每日口语课
class DailySpeakingLesson: UITableViewCell {
weak var delegate: DailySpeakingLessonDelegate?
@IBAction func buttonPressed() {
delegate?.dailySpeakingLessonButtonPressed()
}
}
Delegate 代表
protocol DailySpeakingLessonDelegate: class {
func dailySpeakingLessonButtonPressed()
}
The best practice is to use delegate pattern. 最佳实践是使用委托模式。 If you have a button in DemoTableViewCell which you are using in BaseTableViewController make a protocol BaseTableViewCellDelegate and assign delegate of BaseTableViewCell to BaseTableViewController so that base viewcontroller is notified button is pressed in the cell.
如果在DemoTableViewCell中有一个要在BaseTableViewController中使用的按钮,请制定协议BaseTableViewCellDelegate并将BaseTableViewCell的委托分配给BaseTableViewController,以便通知基本ViewController在单元格中被按下。
protocol DemoTableViewCell Delegate: class {
func didTapDemoButton(onCell: DemoTableViewCell)
}
class DemoTableViewCell: UITableViewCell {
weak var delegate: DemoTableViewCellDelegate?
@IBAction func demoButtonAction(_ sender: UIButton) {
delegate?.didTapDemoButton(onCell: self)
}
}
class BaseTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: DemoTableViewCell), for: indexPath)
cell.delegate = self
return cell
}
}
extension BaseTableViewController: DemoTableViewCellDelegate {
func didTapDemoButton(onCell: DemoTableViewCell) {
//Whenever the button in cell is pressed this delegate method gets called because we have set delegate of DemoTableViewCell as of the base view controller.
//now you can do here whatever you want when button is pressed.
tableView?.reloadData()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.