2016-02-03 66 views
2

我的數據模型定製獲取核心數據(SWIFT)

enter image description here

取FUNC

func fetchAndSetResults2(){ 
    let dataController = DataController.sharedController 
    let moc = dataController.managedObjectContext 
    let request = NSFetchRequest(entityName: "Cart") 
    let formatSort = NSSortDescriptor(key: "cartId", ascending: true) 
    request.sortDescriptors = [formatSort] //[formatSort, nameSort] 
    request.predicate = NSPredicate(format: "cartToUser.userId = %@", serverUserId) 


    fetchedResultController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath: "cartId", cacheName: nil) 
    fetchedResultController.delegate = self 

    do { 
     try fetchedResultController.performFetch() 
    } 
    catch { 
     fatalError("Error in fetching records") 
    } 
} 

截圖 enter image description here

更新:排

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    if let sections = fetchedResultController.sections { 
     return sections.count 
    } 
    return 0 
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if let sections = fetchedResultController.sections { 
     let currentSection = sections[section] 
     return currentSection.numberOfObjects 
    } 
    return 0 
} 

的數量我在覈心數據新。我已經從購物車實體獲取數據。我的表格視圖看起來像截圖。看看Cart-user(38)-363690,它顯示1個購物車Id中的2個單元格,因爲它有2張照片。如何只顯示一個單元,即使它有兩張或更多照片?

+0

問題可能與數量的切片和numberofrowsinsection方法。請在這裏添加方法 – Johnykutty

+0

好吧,請檢查我編輯的問題@Johnykutty –

+0

購物車用戶(38)-XXXXX是表格視圖單元格或其部分標題的一部分? – Johnykutty

回答

0

從這我所能理解的是,如果照片是有車,它應該只顯示一行,並顯示單元格中的照片的計數。爲此,numberOfRowsInSection方法應該只返回1.不需要返回圖像數量。並在cellForRowAtIndexPath方法根據該部分中的照片數量配置行。

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    if let sections = fetchedResultController.sections { 
     return sections.count 
    } 
    return 0 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) 
    let title: String 
    if let sections = fetchedResultController.sections { 
     let currentSection = sections[indexPath.section] 
     title = "\(currentSection.numberOfObjects) Photos" 
    } 
    else { 
     title = "0 Photos" 
    } 
    //set the label title for photos 

    return cell 
} 
+0

嘿謝謝你的回答。它的工作原理,但有沒有辦法做到這一點,而無需更改該部分?我的意思是我只使用部分來指導我,並計劃稍後隱藏sectionTile。 –

+0

我無法得到您需要的完整背景。所以這是我根據我的理解保證的方法 – Johnykutty