2015-12-02 49 views
1

我有一些代码的问题,它确实在xCode 6(使用ios 8.1)中工作,但没有使用7.1(和ios 9.1)。Swift 2:Table view cellForRowAtIndexPath - BAD_EXEC

我有一个表视图与自定义单元填充信息从核心数据。在我的测试中,Szenario 1入围是coredata。在每个单元格中,我都会对另一个核心数据实体进行计数(表示下一个详细视图中相关条目的数量)。在我的testszenario中,这个实体没有相关的条目。

不幸的是,我用xcode 7.1得到一个错误(意外发现无解包装可选值),无法确定它来自哪里。

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 
} 

回答

0

你应该永远不会做任何你fetchRequest内的cellForRowAtIndexPath - 因为这会使你的应用程序挂起(重装一直是你的数据,同时滚动)。

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

像:

viewDidLoad中:

负载核心数据对象

刷新的TableView

+0

你probalby权,但是这可能不是原因的BAD_EXEC,是吗? –

+0

它可能是。因为抓取很慢。如果在“NumberOfRowsInTableView”中的对象与cellForRowAtIndexPath中的NSManagedObjectArray不相同,则会计数 - 这会导致相同的错误。我认为这是你的问题,我会开始解决这个问题。 – derdida