ios - 重新加载数据时 Tableview 为零
问题描述
所以我有一个表格视图,里面有两个单元格。一个是拨动开关,禁用时会将第二个单元格中的文本颜色更改为灰色。我在其自定义单元格类中处理该单元格的开关,并在表的类中为其设置一个布尔值,didSet
以触发重新加载表格视图的函数。问题是我链接到情节提要的表格视图在重新加载数据时为零,但只有在从didSet
. 当通过其他方式正常调用它时,viewDidLoad()
它会按预期工作。完整的错误是:Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
class Notifications: UIViewController {
var notificationsEnabled:Bool = true {
didSet {
RefreshTable()
}
}
@IBOutlet weak var notificationsTableView: RoundedTable!
var notificationItems = ["Notifications", "Time"]
override func viewDidLoad() {
super.viewDidLoad()
notificationsTableView!.dataSource = self
notificationsTableView!.delegate = self
}
func RefreshTable() {
notificationsTableView.reloadData() //Crashes here
}
}
extension Notifications: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notificationItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "notificationToggleCell", for: indexPath) as! NotificationToggleCell
cell.notificationToggleLbl.text = notificationItems[indexPath.row]
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "notificationTimeCell", for: indexPath) as! NotificationTimeCell
cell.notificationTimeLbl.text = notificationItems[indexPath.row]
if (!notificationsEnabled) {
cell.notificationTimeLbl.textColor = .gray
cell.notificationTimeTxt.textColor = .gray
}
return cell
default:
return UITableViewCell()
}
}
}
这是自定义单元格类:
class NotificationToggleCell: UITableViewCell {
@IBOutlet weak var notificationToggleLbl: UILabel!
@IBOutlet weak var notificationToggleSwitch: UISwitch!
let storyboard = UIStoryboard(name: "Main", bundle: nil)
@IBAction func notificationSwitchChanged(_ sender: Any) {
let notificationsViewController = storyboard.instantiateViewController(withIdentifier: "Notifications") as! Notifications
if (notificationToggleSwitch.isOn) {
notificationsViewController.notificationsEnabled = true
}
if (!notificationToggleSwitch.isOn) {
notificationsViewController.notificationsEnabled = false
}
}
}
解决方案
尝试使用委托设计模式。
protocol NotificationsDelegate {
func toggleSwitched()
}
在您的 NotificationToggleCell 添加
var toggleDelegate: NotificationsDelegate?
@objc func toggleSwitched() {
toggleDelegate.toggleSwitched()
}
在 cellForRowAt 函数中
cell. toggleDelegate = self
最后
extension Notifications: NotificationsDelegate {
func toggleSwitched() {
RefreshTable()
}
}
推荐阅读
- ajax - 我想使用 ajax 监控两个文本文件
- asp.net-core - 根据 FileHelpers 示例,如何使用 GetRandomFileName 保存上传的文件以供使用?
- javascript - 无法在应用程序开发工具(javascript)中获取订单按钮的属性值
- python-3.x - 在 python3 中创建十六进制校验和的问题
- mysql - 如何解决 Laravel 6 中的 MariaDB 语法错误?
- hdfs - 调试错误 [Vertica][VJDBC](100172) 一行或多行被服务器拒绝
- python - 有没有办法为子图绘制一条箭头线?
- firebase - 如何使用 Nuxt SSR 设置 Firebase 离线身份验证
- jenkins - 如何创建待处理作业队列大小的数量?
- python - 无法使用 matplotlib 在 Python 中绘制列表元组