2009-10-15 5 views
1

J'ai récemment téléchargé exemple d'Apple app CoreDataBooks et pour l'intérêt de l'apprentissage ont décidé d'ajouter une cellule au dernier enregistrement dans le RootViewController.m vue de tableau, qui affiche le nombre d'enregistrements qui ont été récupérés.CoreDataBooks Ajouter nouvelle cellule à tableview

Le code que j'ai ajouté est illustré ci-dessous. Je n'ai pas eu le temps d'ajouter le nombre de fetch parce que je reçois de mauvais résultats de mon code. Si vous téléchargez CoreDataBooks et remplacez les méthodes de source de données de la vue table par le code, vous verrez ce qui suit:

  1. La nouvelle cellule est ajoutée au bas de la vue de table.
  2. Lorsque vous revenez en haut de la table, une autre cellule reçoit le format qui est supposé être donné à la dernière cellule de la table. Dans mon cas, dans la section auteur de 'Bill Bryson', le titre du livre 'Notes d'un grand pays' devient bleu et est centré lorsque vous revenez en haut de la table.

Une aide pour résoudre ce problème serait appréciée.

Voici le lien pour l'application échantillon CoreDataBooks: link text

Voici le code modifié pour 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 @""; 

} 

C'est tout. Merci pour votre temps.

Répondre

3

Je vois deux solutions:

  1. Utilisez un identifiant de cellule différent pour votre cellule.

Pour ce faire, définir un nouvel identifiant de cellule après l'autre et déplacer le code de réutilisation des cellules dans les deux cas cellulaires différentes:

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. Remplacer tout le formatage applicable lors de la configuration des cellules:

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

Excellent! Fait du sens et fonctionne bien. Je suis allé avec l'option # 1. Merci. – CraigH

Questions connexes