如何防止由于TableView中未捕获的异常'CALayerInvalid'错误而终止应用程序

时间:2019-02-14 17:15:36

标签: ios swift tableview

我有一个带有基本样式单元格的简单TableView。我按照以下步骤填写

类MunicipalitiesViewController:UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate {

var municipalities = [Municipality]()

@IBOutlet weak var municipalitiesTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    let dbHelper = RaccoltacaseDbHelper()
    municipalities = dbHelper.getMunicipalities()

    municipalitiesTableView.dataSource = self
    municipalitiesTableView.delegate = self        
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    filterMunicipalities(s: searchText)

    self.municipalitiesTableView.reloadData()
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
    return self.municipalities.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

    var municipality = Municipality()

    cell.textLabel?.text = municipality.value!

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let filterVC = storyboard?.instantiateViewController(withIdentifier: "FilterViewController") as! FilterViewController

    var municipality = Municipality()
    if municipalitiesSearchBar.text != "" {
        municipality = filteredMunicipalities[indexPath.row]
    }
    else {
        municipality = municipalities[indexPath.row]
    }

    let search = Search()
    if (municipality.id == 0) {
        search.zoneId = municipality.province
        search.isMunicipality = false
    }
    else {
        search.zoneId = municipality.id
        search.isMunicipality = true
    }
    search.type = Search.ZONE_TAG

    filterVC.search = search

    self.navigationController?.pushViewController(filterVC, animated: true)
}

}

现在,当我单击一行时,所有功能均按预期工作,但如果单击单元格分隔器应用程序崩溃,并出现以下错误:由于未捕获的异常'CALayerInvalid',终止应用程序,原因:'图层是其循环的一部分层树'

我在做什么错了?

0 个答案:

没有答案