2013-08-21 64 views
9

Tôi có một thực thể được hiển thị trong chế độ xem bảng chỉ trong một phần. Thực thể có hai thuộc tính, workoutNametrainingLevel. Cả hai đều thuộc loại chuỗi. Cấp độ đào tạo bao gồm 3 loại: 1, 2, 3. (trainingLevel = (Integer 16 hoặc String Type? Điều gì sẽ là lý tưởng?) Tôi muốn chia bảng thành ba phần, mỗi phần chứa các mục cho cấp độ đào tạo tương ứng .?Các phần UITableView của iOS có fetchedResultsTự động của sự nhầm lẫn

làm thế nào để làm điều này mã tôi hiện đang sử dụng là dưới đây:

- (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); 
    } 
} 

những gì tôi đang phải vật lộn với là:

  • làm thế nào để xác định số lượng hàng cho mỗi phần thông qua mô hình dữ liệu cốt lõi bằng cách tìm nạp số lượng mục có cấp độ đào tạo 1 hoặc 2 hoặc 3.
  • Cách điền vào các hàng của từng phần bằng cách tìm nạp các mục chính xác.
  • Cách đặt tiêu đề cho từng tiêu đề phần.
+3

Bạn đang ở đó khá nhiều. Chỉ cần kiểm tra tài liệu cho 'NSFetchedResultsController' cụ thể là tham số' sectionNameKeyPath' của initialiser được chỉ định, mà bạn muốn đặt làm thuộc tính 'trainingLevel' của bạn. – Rog

+0

Cũng lưu ý rằng getter 'fetchedResultsController' của bạn nên thực hiện tìm nạp, không phải' fetchWorkoutSet', nếu không bạn sẽ có hai cuộc gọi khác nhau nếu bạn cần đảm bảo tập hợp đã được tìm nạp và bạn cần fetchedResults. – memmons

Trả lời

17

Đây là một hướng dẫn tốt về việc sử dụng fetchedResultsControllers: http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

Tạo một số đặc tính giữ ngữ cảnh của bạn và lấy:

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

Trong tài sản fetchedResultsController của bạn, sử dụng sectionKeyNamePath để thiết lập kết quả lấy của bạn trong các phần:

- (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; 

} 

dân ban đầu của bạn của fetchedResultsController của bạn có thể xảy ra trong bạn -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 
    } 
} 

Sau đó bạn muốn trả lại số phận và số lượng hàng trong các phần như thế này:

- (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];   
} 

Sau đó, bạn có thể nhận được quản lý của bạn đối tượng cho hàng cụ thể như sau:

- (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

Tôi đã thử ở trên mà không có may mắn, Chủ yếu là gặp rắc rối về phía cuối mà bạn đã nói để sử dụng NSManagedObject .... Không chắc chắn làm thế nào tôi có thể nhận được dữ liệu của tôi thông qua đó ... Cảm ơn – user2512523

+1

@ user2512523 Cập nhật với mã bổ sung để làm cho nó rõ ràng hơn. Các đối tượng 'WorkoutSet' của bạn là những gì được trả về bởi' fetchedResultsController'. – memmons

+0

@ user2512523 Nếu câu trả lời này đã giúp bạn, hãy đánh dấu nó là được chấp nhận để câu hỏi không hiển thị vẫn mở. – memmons