开发者

CoreDataBooks Add new cell to tableview

开发者 https://www.devze.com 2022-12-08 08:26 出处:网络
I recently downloaded Apple\'s sample app CoreDataBooks and for the sake of learning decided to add a cell to the last record in the RootViewController.m table view, which would display the number of

I recently downloaded Apple's sample app CoreDataBooks and for the sake of learning decided to add a cell to the last record in the RootViewController.m table view, which would display the number of records that were fetched.

The code I added is shown below. I didn't get to the point of adding the fetch count because I am getting bad results from my code. If you download CoreDataBooks and replace the table view datasource methods with the code here you will see the following:

  1. the new cell is added to the bottom of the table view.
  2. when you scroll back to the top of the table another cell receives the formatting that is only supposed to be given to the last cell of the table. In my case, under the author section of 'Bill Bryson' the book title 'Notes From a Big Country' turns blue and is centered when you scroll back to the top of the table.

Any help in resolving this would be appreciated.

Here is the link for the sample app CoreDataBooks: link text

Here is the modified code for 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 = [[fetch开发者_Python百科edResultsController   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 @"";

}

That's all. Thank-you for your time.


I see two solutions:

  1. Use a different cell identifier for your cell.

To do this, define a new cell identifier after the other and move the cell reuse code inside the two different cell cases:

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 . Override all of the applicable formatting when configuring the cells:

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消