swift 2 : Table view cellForRowAtIndexPath - BAD_EXEC

标签 swift core-data tableview

我遇到了一些代码的问题,这些代码确实在 xCode 6(使用 ios 8.1)中有效,但不适用于 7.1(和 ios 9.1)。

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

不幸的是,我在 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
}

最佳答案

你永远不应该在你的 cellForRowAtIndexPath 中做任何 fetchRequest - 因为这会让你的应用程序挂起(滚动时总是重新加载你的数据)。

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

喜欢:

ViewDidLoad:

Load Core Data Objects

Reload TableView

关于 swift 2 : Table view cellForRowAtIndexPath - BAD_EXEC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037693/

相关文章:

swift - 由于SKCameraNode导致场景切换时横幅广告移动

arrays - 无法调用非函数类型的值 '[String]'

javascript - 如何制作动态表

swift - 如何让 UITableView 的固定标题在滚动后隐藏

ios - arm64 iOS Swift Gstreamer 库不支持 rtspsrc (Xcode 10.1)

macos - 如何检测弹出窗口是否已完成?

ios - Core-Data NSFetchedResultsController 对多关系

objective-c - NSMigrationManager 'Model already contains an entity named <Entity Name>'

ios - 核心数据、Xcode 4.5 和在数据模型中创建获取的请求

iOS7 Tableview删除行最佳实践