![](/img/trans.png)
[英]UITableview : Change cell height dynamically when clicked the custom button
[英]Reload UITableView when button clicked in custom UITableViewCell
我的表格视图单元格中有一个按钮,我想重新加载整个视图,这是基本控制器。
此类是我想要重新加载的类(刷新,也许重新调用视图控制器)。
import UIKit
class TableVC: BaseController, DBDelegate, PLDelegate {
@IBOutlet weak var tableViewDB: UITableView!
}
这是我必须执行的操作:
import UIKit
class DailySpeakingLesson: UITableViewCell {
}
为此使用委托 。
在tableView(_:cellForRowAt:)
设置自定义单元的委托,然后在委托的函数内调用tableViewDB.reloadData()
。
桌面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()
}
}
每日口语课
class DailySpeakingLesson: UITableViewCell {
weak var delegate: DailySpeakingLessonDelegate?
@IBAction func buttonPressed() {
delegate?.dailySpeakingLessonButtonPressed()
}
}
代表
protocol DailySpeakingLessonDelegate: class {
func dailySpeakingLessonButtonPressed()
}
最佳实践是使用委托模式。 如果在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.