【问题标题】:Swift 2: Table view cellForRowAtIndexPath - BAD_EXECSwift 2:表格视图 cellForRowAtIndexPath - BAD_EXEC
【发布时间】:2015-12-02 08:09:55
【问题描述】:

我对某些代码有疑问,它确实在 xCode 6(使用 ios 8.1)中有效,但不适用于 7.1(和 ios 9.1)。

我有一个表格视图,其中包含一个由核心数据中的信息填充的自定义单元格。在我的测试中,Szenario 1 条目位于 coredata 中。在每个单元格中,我对另一个核心数据实体进行计数(表示下一个详细视图中相关条目的数量)。在我的测试中,这个实体没有相关条目。

不幸的是,我在使用 xcode 7.1 时遇到了一个错误(在展开可选值时意外发现 Nil),无法分辨它来自哪里。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UI_Buyer_My_Bubbles_Cell  
    if let buyers_bubbles = fetchedResultsController?.objectAtIndexPath(indexPath) as? Buyer_My_Bubbles {

        cell.label_seller?.text = buyers_bubbles.user_seller_name
        cell.label_points?.text = "\(buyers_bubbles.points_buyer_collected) bubbles"+"\r\n"+"gesammelt"
        cell.label_image?.image = UIImage(data:buyers_bubbles.user_seller_logo!)

        if ((buyers_bubbles.user_seller_no_devices=="1") || (cell.label_image.image == nil)) {
            cell.label_image?.image = UIImage(named: "no_logo")
        }

        //Count the number of offers
        var offer_count = 0

        do {

        let managedObjectContext2 = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
        let fetchRequest = NSFetchRequest(entityName: "Buyer_My_Offers")
        var error: NSError?
        let fetchedResults = try managedObjectContext2!.executeFetchRequest(fetchRequest) as? [Buyer_My_Offers]
        if let results = fetchedResults {
        if results.count==0{
            for (var i=0; i < results.count; i++)
                {
                    if (results[i].offer_seller_id == buyers_bubbles.user_seller_id) {
                        offer_count = offer_count + 1
                    }
                }
            }
        }

            if offer_count == 1 {
                cell.label_no_offers.text = "\(String(offer_count)) Prämie"
            }
            if offer_count == 0 {
                cell.label_no_offers.text = "Keine "+"\r\n"+"Prämien"
            }
            if offer_count > 1 {
                cell.label_no_offers.text = "\(String(offer_count)) Prämien"
            }

        }
catch {print("Unable to complete request. \(error)")}

   }
    return cell
}

【问题讨论】:

    标签: swift core-data tableview


    【解决方案1】:

    您永远不应该在 cellForRowAtIndexPath 中执行任何 fetchRequest - 因为这会使您的应用程序挂起(在滚动时始终重新加载您的数据)。

    在 ViewDidLoad 中加载您的结果,并在需要时刷新它们。

    喜欢:

    ViewDidLoad:

    加载核心数据对象

    重新加载表格视图

    【讨论】:

    • 你说得对,但这可能不是 BAD_EXEC 的原因,是吗?
    • 有可能。因为获取速度很慢。如果您在“NumberOfRowsInTableView”中的对象与您在 cellForRowAtIndexPath 中的 NSManagedObjectArray 不同,那么如果有计数 - 这将带来相同的错误。我认为这是你的问题,我会先解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多