【问题标题】:When I try to make reloadData() I get a crash当我尝试制作 reloadData() 时,我遇到了崩溃
【发布时间】:2016-09-12 12:36:14
【问题描述】:

当我尝试重新加载 tableview 时,我收到一个类似

的错误

libc++abi.dylib:以 NSException 类型的未捕获异常终止

我的UITableViewController

class NewTableViewController: UITableViewController {

var videos = [Video]()
    {
    didSet {

            self.tableView.reloadData() //error here
    }
}
let URL = "https://api.vid.me/channel/1/new"

override func viewDidLoad() {
    super.viewDidLoad()

    let request = Alamofire.request(.GET, self.URL)
        request.responseObject { (response: Response<NewestVideos, NSError>) in

        switch response.result {
        case .Success(let newest):
            self.videos = newest.videos!
        case .Failure(let error):
            print("Request failed with error: \(error)")
        }
    }
}

override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewTableViewCell
    let video = videos[indexPath.row]
    cell.newVideoNameLabel?.text = video.completeUrl != nil ? video.completeUrl! : "nil"
     return cell
     }
}

我认为这是线程的问题? 在我的应用程序中,我发出一个 .get 请求,如果我尝试在 didSet 中打印,例如 print(self.videos),我会得到一个值 我尝试添加

dispatch_async(dispatch_get_main_queue()) {
   self.tableView.reloadData()
}

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    libc++abi.dylib: terminating with uncaught exception of type NSException 错误发生在任何方面。

    • outlet 中删除引用
    • performSegueWithIdentifier 中的坏名
    • IBActions 类型。

    查看此link 了解更多信息

    【讨论】:

    • 还有另一种可能发生此错误的方式,对我来说,这是我在此调用 let cancelAction = UIAlertAction(title: nil, style: .Cancel) { action in ... 中忽略了某些内容。即使title 参数是String? 类型,您也不应该使用nil(在运行Xcode 8 和Swift 2.3 旧模式时)。实际上不应该是nil。狩猎愉快。自 Xcode 8 以来,这个错误消息一直是最让我困惑的。我的示例的工作代码是使用实际的String:let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { action in ...
    【解决方案2】:

    确保当您设置videos 时,您是在Main Queue 中调用它

    您可以在设置Main Queue 时保护videos

    dispatch_async(dispatch_get_main_queue()) {
       self.videos = newest.videos!
    }
    

    或者你可以保护它:

    didSet {
        dispatch_async(dispatch_get_main_queue()) {
            self.tableView.reloadData()
        }
    }
    

    【讨论】:

    • @DmitrievichR 不在get中,因为要在设置var时重新加载数据(didSet)
    • 哦不明白{}我的意思是 didSet{} 但没关系我现在也崩溃了 var videos = [Video](){ didSet { dispatch_async(dispatch_get_main_queue()) { self.tableView.reloadData() } } }
    • 然后你可以检查jose920405's answer是否设置正确。检查你的 IB
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 2015-12-28
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多