我正在使用Apple的CoreDataBooks示例项目,我希望在tableview中的indexPath 0上有一个自定义单元格,然后从索引1开始获取我的核心数据结果。
我尝试了一些不同的解决方案,但无法使其正常运行,如果您有任何想法,我们将非常感谢。
如果你想知道我尝试过但失败了,请告诉我。我想要实现的目标看起来很简单,只需在单元格1而不是0开始获取结果。
编辑2:
我的所有UITableViewDataSource和configureGuestCell代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
static NSString *CellIdentifier = @"statsCell";
GuestStatsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[GuestStatsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Configure the cell.
[self configureStatsCell:cell];
return cell;
}
if (indexPath.row > 0)
{
static NSString *CellIdentifier = @"guestCell";
customGuestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[customGuestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
[self configureGuestCell:cell atIndexPath:indexPath];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int cellHeight;
if (indexPath.row == 0)
{
cellHeight = 240;
}
else
{
cellHeight = 44;
}
return cellHeight;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[fetchedResultsController sections] count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (void)configureGuestCell:(customGuestCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
//Configure the cell to show the Guests first and last name and other details
GuestInfo *guest = [fetchedResultsController objectAtIndexPath:indexPath];
cell.guestNameLbl.text = [NSString stringWithFormat:@"%@ %@", guest.firstName, guest.lastName];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the managed object.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error])
{
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create and push a detail view controller.
guestListDetailViewController *detailViewController = [[guestListDetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
GuestInfo *selectedGuest = (GuestInfo *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
// Pass the selected book to the new view controller.
detailViewController.guest = selectedGuest;
[self.navigationController pushViewController:detailViewController animated:YES];
}
答案 0 :(得分:1)
固定代码:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects] +1;
}
- (void)configureGuestCell:(customGuestCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *path = [NSIndexPath indexPathForRow:(indexPath.row - 1) inSection:indexPath.section]
//Configure the cell to show the Guests first and last name and other details
GuestInfo *guest = [fetchedResultsController objectAtIndexPath:path];
cell.guestNameLbl.text = [NSString stringWithFormat:@"%@ %@", guest.firstName, guest.lastName];
}
小解释:
你应该在numberOfRowsInSection中多返一行,但为了防止错误,在调用[NSFetchedResultsController objectAtIndexPath]之前应该减少行号。
答案 1 :(得分:0)
将1添加到indexPath.row值....?!
答案 2 :(得分:0)
为此,您还需要编辑-tableView: numberOfRowsInSection:
以返回(realRows + 1)。
同样,每当您引用NSFetchedResultsController
|时,您都希望减少一行。我怀疑NSArray
configureCell:atIndexPath:
您希望/需要触摸数据源的任何地方,您需要通过递增或递减来调整该行。
不要更改NSIndexPath
,没有必要。调整-configureCell: atIndexPath:
或其他任何接触该索引值的索引:
-(void)configureCell:(id)cell atIndexPath:(NSIndexPath*)indexPath
{
NSInteger finalIndex = [indexPath row] - 1;
NSIndexPath *newPath = [NSIndexPath indexPathForRow:finalIndex inSection:[indexPath section]];
id object = [[self myNSFRC] objectAtIndexPath:newPath];
//Continue configuring your cell
}
显然,如果您使用的是阵列,请不要费心去创建新的NSIndexPath
。