【发布时间】:2009-10-15 06:07:44
【问题描述】:
我最近下载了 Apple 的示例应用程序 CoreDataBooks,为了学习,决定在 RootViewController.m 表格视图的最后一条记录中添加一个单元格,它将显示获取的记录数。
我添加的代码如下所示。我没有达到添加提取计数的地步,因为我的代码得到了不好的结果。如果您下载 CoreDataBooks 并将表格视图数据源方法替换为此处的代码,您将看到以下内容:
- 新单元格被添加到表格视图的底部。
- 当您滚动回表格顶部时,另一个单元格接收到的格式应该只提供给表格的最后一个单元格。就我而言,在“Bill Bryson”的作者部分下,当您滚动回表格顶部时,书名“来自大国的笔记”会变为蓝色并居中。
任何解决此问题的帮助将不胜感激。
这里是示例应用 CoreDataBooks 的链接:link text
这是 RootViewController.m 的修改代码:
/*
The data source methods are handled primarily by the fetch results controller
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[fetchedResultsController sections] count] + 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section < [[fetchedResultsController sections] count])
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
return 1;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@"indexPath.section: %i",indexPath.section);
if (indexPath.section < [[fetchedResultsController sections] count]){
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
}
else{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = (@"This is a NEW Cell...");
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.textColor = [UIColor blueColor];
}
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell to show the book's title
Book *book = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = book.title;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
if (section < [[fetchedResultsController sections] count])
return [[[fetchedResultsController sections] objectAtIndex:section] name];
return @"";
}
就是这样。谢谢您的宝贵时间。
【问题讨论】:
标签: iphone uitableview core-data