2013-08-21 69 views
9

我有一個實體顯示在一個表格視圖中只有一個部分。該實體有兩個屬性,workoutNametrainingLevel。兩者都是字符串類型。訓練級別由3種類型組成:1,2,3(trainingLevel =(整數16或字符串類型?哪種理想?)我想將表格分成三部分,每部分包含相應訓練級別的條目?iOS的UITableView部分與fetchedResultsController混淆

如何做到這一點我目前正在使用的代碼如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.workoutType.workouts.count; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
            reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 


    WorkoutSet *workoutSet = [self.fetchedResultsController objectAtIndexPath:indexPath]; 


    cell.textLabel.text = workoutSet.workoutName; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", workoutSet.days.count];  
} 

-(void)fetchWorkoutSets 
{ 

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkoutSet"]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"workoutType = %@", self.workoutType]; 

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"workoutName" ascending:YES]; 
    [fetchRequest setSortDescriptors:@[sortDescriptor]]; 
    [fetchRequest setPredicate:predicate]; 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] 
           initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext 
           sectionNameKeyPath:nil cacheName:nil]; 

    NSError *error; 
    if (![self.fetchedResultsController performFetch:&error]) 
    { 
     NSLog(@"Fetch failed: %@", error); 
    } 
} 

我所掙扎的是:

  • 如何確定的行數每節通過核心數據模型獲取培訓級別爲1或2或3的條目數。
  • 如何通過提取正確的項目來填充每個部分的行。
  • 如何爲每個節標題賦予標題。
+3

你幾乎在那裏。只需檢查「NSFetchedResultsController」的文檔,特別是您想要設置爲「trainingLevel」屬性的指定初始化程序的「sectionNameKeyPath」參數。 – Rog

+0

另請注意,您的'fetchedResultsController' getter應該執行提取操作,而不是'fetchWorkoutSet',否則如果您需要確保已經提取了鍛鍊集並且您需要fetchedResults,則會有兩個不同的調用。 – memmons

回答

17

下面是使用fetchedResultsControllers一個很好的教程:http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

創建一些特性來保存您的上下文並獲取:

@property (nonatomic,strong)NSManagedObjectContext* managedObjectContext; 
@property (nonatomic,retain)NSFetchedResultsController *fetchedResultsController; 

在你fetchedResultsController屬性,使用sectionKeyNamePath設置您獲取的成果部分:

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
              entityForName:@"Workouts" 
            inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
     initWithKey:@"workoutName" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
     [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
      managedObjectContext:managedObjectContext 
       sectionNameKeyPath:@"trainingLevel" 
         cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 

} 

你對你fetchedResultsController的初始羣體可以在-viewDidLoad:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSError *error; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Update to handle the error appropriately. 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 
} 

發生,那麼你會返回的行部分的數量和數量的部分是這樣的:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[self.fetchedResultsController sections] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    id <NSFetchedResultsSectionInfo> sectionInfo = 
     [[[self fetchedResultsController] sections] objectAtIndex:section]; 

    return [sectionInfo numberOfObjects];   
} 

然後,您可以讓您的管理對象爲這樣的特定行:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    // init the cell 
    // and whatever other setup needed 

    WorkoutSet *workoutSet = 
     [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    // configure the cell from the managedObject properties 
} 
+0

我已經嘗試了上面沒有運氣,主要有麻煩的結束,你已經說過使用NSManagedObject ....不知道如何我可以通過該數據得到我的數據...謝謝 – user2512523

+1

@ user2512523更新了額外的代碼使其更清晰。你的'WorkoutSet'對象是'fetchedResultsController'返回的。 – memmons

+0

@ user2512523如果此答案對您有幫助,請將其標記爲已接受,以便問題不會顯示爲仍然顯示。 – memmons