【问题标题】:CoreDataBooks Add new cell to tableviewCoreDataBooks 将新单元格添加到 tableview
【发布时间】:2009-10-15 06:07:44
【问题描述】:

我最近下载了 Apple 的示例应用程序 CoreDataBooks,为了学习,决定在 RootViewController.m 表格视图的最后一条记录中添加一个单元格,它将显示获取的记录数。

我添加的代码如下所示。我没有达到添加提取计数的地步,因为我的代码得到了不好的结果。如果您下载 CoreDataBooks 并将表格视图数据源方法替换为此处的代码,您将看到以下内容:

  1. 新单元格被添加到表格视图的底部。
  2. 当您滚动回表格顶部时,另一个单元格接收到的格式应该只提供给表格的最后一个单元格。就我而言,在“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


    【解决方案1】:

    我看到了两种解决方案:

    1. 为您的单元格使用不同的单元格标识符。

    为此,在另一个之后定义一个新的单元标识符,并将单元重用代码移动到两个不同的单元格中:

    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) {
    

    2 。配置单元格时覆盖所有适用的格式:

    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
    }
    

    【讨论】:

    • 太棒了!有道理,效果很好。我选择了选项#1。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    相关资源
    最近更新 更多