【发布时间】: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
}
【问题讨论】: