Swift UIViewController tableView.reloadData在第二次调用时包装nil

时间:2016-11-03 15:42:43

标签: ios uitableview

许多搜索没有成功。在viewDidLoad的视图控制器中,我调用了一个名为callJson的函数。然后它设置绑定到UITableView的数据。这一切都很有效。现在我也有一个UIRefreshControl,当我拉它时,它会调用同一个函数callJson,而在第二次尝试时我会得到臭名昭着的错误

致命错误:在展开Optional值时意外发现nil on self.tableView.reloadData()

这是向json发出请求的函数,并说要用新数据重新加载表。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let alertD = alerts?.Alerts[indexPath.row] {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "AlertTableViewCell") as! AlertTableViewCell
        cell.name.text = alertD.Summary
        cell.icon.image = Helpers.listImage24dp(id: alertD.TOA)
        cell.selectionStyle = .none
        if alertD.Url.characters.count == 0 {
            cell.isUserInteractionEnabled = false
            return cell
        }
        cell.isUserInteractionEnabled = true
        cell.name.textColor = UIColor.blue
        return cell
    }
    return UITableViewCell()
}

tableView设置单元格数据

var features = new ol.Collection();
vectorSource = new ol.source.Vector({ features: features }),
vectorLayer = new ol.layer.Vector({
source: vectorSource
}),
styles = {
    route: new ol.style.Style({
        stroke: new ol.style.Stroke({
            width: 4,
            color: [255, 120, 0, 0.8]
    })
}),
routeSR: new ol.style.Style ({
    stroke: new ol.style.Stroke({
        width: 6,
        color: [0, 0, 0, 0.8]
    })
}),
icon: new ol.style.Style({
    image: new ol.style.Icon({
        anchor: [0.5, 1],
        src: session.icon_url
        })
    })
};



//CREATE THE MAP AND STORE IN SESSION.
session.map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
          source: new ol.source.OSM()
      }),
      vectorLayer
    ],
    view: new ol.View({
        center: [-5685003, -3504484],
        zoom: 11
    })
});

当我在self.tableView.reloadData()之前设置一个断点并进入时,它会进入定义的插座但是会抛出nil错误。我不确定我是否需要在tableView或其他东西中设置不同的东西,因为它第一次正常工作。任何帮助/想法尝试都值得赞赏。

1 个答案:

答案 0 :(得分:1)

请使用以下内容。

DispatchQueue.main.async {
     self.tableview.reloadData()
}

原因: tableview重新加载应该在UI线程中发生。