0
核心數據中有兩個不相關的實體。我希望在一個UITableView
(以及能夠從UITableView
添加或刪除它們)中以兩個部分顯示這些實體的樣本。如何在「tableview」的單獨部分中顯示來自「coredata」的幾個實體?
我該如何實現這個任務使用一個UIFetchedResultsController
甚至可以只用一個獲取的結果控制器來完成這個任務。
UPDATE
使用2個fetchedResultsControllers,但我有錯誤:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (1) beyond bounds (1)'
這是我的實現代碼:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
Education* education = [self.fetchedResultsController1 objectAtIndexPath:indexPath];
cell.textLabel.text = education.educationType;
} else {
FamilyStatus* familyStatus = [self.fetchedResultsController2 objectAtIndexPath:indexPath];
cell.textLabel.text = familyStatus.familyStatusType;
}
}
#pragma mark - UITableViewDataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1) {
NSInteger rowsCount;
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController1 sections] objectAtIndex:0];
if (self.isEdit) {
rowsCount = [sectionInfo numberOfObjects] + 1;
} else {
rowsCount = [sectionInfo numberOfObjects];
}
return rowsCount;
} else {
NSInteger rowsCount;
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController2 sections] objectAtIndex:0];
if (self.isEdit) {
rowsCount = [sectionInfo numberOfObjects] + 1;
} else {
rowsCount = [sectionInfo numberOfObjects];
}
return rowsCount;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellIdentifier = @"Cell";
static NSString* addCellIdentifier = @"CellAddButton";
if (indexPath.section == 0) {
if (indexPath.row == 0 && self.isEdit) {
AddViewCell* cell = [tableView dequeueReusableCellWithIdentifier:addCellIdentifier];
if (!cell) {
cell = [[AddViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addCellIdentifier];
}
return cell;
}
else {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (self.isEdit) {
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];
[self configureCell:cell atIndexPath:path];
} else {
[self configureCell:cell atIndexPath:indexPath];
}
return cell;
}
} else if (indexPath.section == 1) {
if (indexPath.row == 0 && self.isEdit) {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"aaa"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aaa"];
}
return cell;
}
else {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"bbb"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bbb"];
}
if (self.isEdit) {
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];
[self configureCell:cell atIndexPath:path];
} else {
[self configureCell:cell atIndexPath:indexPath];
}
return cell;
}
}
return nil;
}
我必須實現5個getter,檢查numberOfRowsInSection中的節號,並以不同的方式爲每個節使用其他方法? – Georgij
或者1個可重複使用的方法和一個小配置,或者製表符和5個表格 – Wain
謝謝。但是有一個錯誤。我究竟做錯了什麼? – Georgij