首页 > 解决方案 > 重新加载数据时 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
        }
    }
}

标签: iosswiftuitableview

解决方案


尝试使用委托设计模式。

protocol NotificationsDelegate {
    func toggleSwitched()
}

在您的 NotificationToggleCell 添加

var toggleDelegate: NotificationsDelegate?

@objc func toggleSwitched() {
    toggleDelegate.toggleSwitched()
}

在 cellForRowAt 函数中

cell. toggleDelegate = self

最后

extension Notifications: NotificationsDelegate { 
    func toggleSwitched() {
        RefreshTable()
    }
}

推荐阅读