Iphone CoreDataBooks向tableview添加新单元格
我最近下载了苹果的示例应用程序CoreDataBooks,为了学习,我决定在RootViewController.m表视图的最后一条记录中添加一个单元格,该单元格将显示获取的记录数 我添加的代码如下所示。我没有添加fetch计数,因为我的代码得到了不好的结果。如果下载CoreDataBooks并用此处的代码替换表视图数据源方法,您将看到以下内容:Iphone CoreDataBooks向tableview添加新单元格,iphone,uitableview,core-data,Iphone,Uitableview,Core Data,我最近下载了苹果的示例应用程序CoreDataBooks,为了学习,我决定在RootViewController.m表视图的最后一条记录中添加一个单元格,该单元格将显示获取的记录数 我添加的代码如下所示。我没有添加fetch计数,因为我的代码得到了不好的结果。如果下载CoreDataBooks并用此处的代码替换表视图数据源方法,您将看到以下内容: 新单元格将添加到表视图的底部 当您滚动回表格顶部时,另一个单元格将接收仅应提供给表格最后一个单元格的格式。在我的例子中,在“Bill Bryson”的
/*
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 @"";
}
/*
数据源方法主要由fetchresults控制器处理
*/
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回[[fetchedResultsController节]计数]+1;
}
//自定义表视图中的行数。
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
如果(节<[[fetchedResultsController节]计数])
{
id sectionInfo=[[fetchedResultsController节]对象索引:节];
返回[sectionInfo numberOfObjects];
}
返回1;
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@“indexPath.section:%i”,indexPath.section);
if(indexath.section<[[fetchedResultsController节]计数]){
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
//配置单元格。
[self-configureCell:cell-atIndexPath:indexPath];
}
否则{
如果(单元格==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
cell.textlab.text=(@“这是一个新单元格…”);
cell.textLabel.textAlignment=UITextAlignmentCenter;
cell.textlab.textColor=[UIColor blueColor];
}
返回单元;
}
-(void)configureCell:(UITableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath{
//配置单元格以显示书籍的标题
Book*Book=[fetchedResultsController对象索引路径:indexPath];
cell.textlab.text=book.title;
}
-(NSString*)tableView:(UITableView*)tableView
标题标题标题部分:(NSInteger)部分{
//将作者姓名显示为章节标题。
如果(节<[[fetchedResultsController节]计数])
返回[[[fetchedResultsController节]对象索引:节]名称];
返回@”;
}
就这些。谢谢您的时间。我看到两种解决方案:
static NSString *CellIdentifier = @"Cell";
static NSString* myCellIdentifier = @"MyCell"; // new cell identifier
// move inside two cell cases
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSLog(@"indexPath.section: %i",indexPath.section);
if (indexPath.section < [[fetchedResultsController sections] count]){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // only reuse this type of cell here
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
}
else{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here
if (cell == nil) {
杰出的这很有道理,而且效果很好。我选择了选项1。多谢各位。
cell.textLabel.text = (@"This is a NEW Cell..."); // You are already overriding the only cell attribute that the book cell configures
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;
cell.textLabel.textAlignment = UITextAlignmentLeft; // I assume Left is the default alignment
cell.textLabel.textColor = [UIColor blackColor]; // I assume black is the default color
}