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, workoutName
và trainingLevel
. 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.
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
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