2011-08-06 5 views
1

Je suis en train d'afficher un TableView personnalisé sur un UIViewController mais je reçois une erreur « UITableView dataSource doit retourner une cellule de tableView: cellForRowAtIndexPath: »TableauViewCells personnalisé sur un UIViewController?

j'avais connecté le TableView à DataSource et délégué.

Une suggestion à propos de l'implémentation ou ai-je besoin d'un UITableViewController?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"CustomCell"; 

CustomCell *cell = (CustomCell *) 
        [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *topLevelObjects = [[NSBundle mainBundle] 
           loadNibNamed:@"CustomCell" 
           owner:nil options:nil]; 
    for (id currentObjects in topLevelObjects){ 
     if ([currentObjects isKindOfClass:[UITableView class]]){ 
      cell = (CustomCell *) currentObjects; 
      break; 
     } 
    }       
} 
//---set the text to display for the cell--- 
cell.cellNameLabel.text = @"This is name"; 
cell.cellValueLabel.text = @"This is Value"; 
return cell; 

Répondre

2

ERREUR:

//NSArray *topLevelObjects = [[NSBundle mainBundle] 
            loadNibNamed:@"CustomCell" 
            owner:nil options:nil]; 

// dans le propriétaire ci-dessus doivent être auto

// if ([currentObjects isKindOfClass:[UITableView class]]){ 

changez cette ligne à

if ([currentObjects isKindOfClass:[CustomCell class]]){ 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"CustomCell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] 
            loadNibNamed:@"CustomCell" 
            owner:self options:nil];//owner should be self 

     for (id currentObjects in topLevelObjects){ 
      if ([currentObjects isKindOfClass:[CustomCell class]]){ 
       cell = (CustomCell *) currentObjects; 
       break; 
      } 
     }       
    } 
     //---set the text to display for the cell--- 
    cell.cellNameLabel.text = @"This is name"; 
    cell.cellValueLabel.text = @"This is Value"; 

    return cell; 

} 
+0

Désolé, je n'ai pas posté le code entier. Je vais éditer ma question. –

+0

après edit dites-moi –

+0

J'ai vu votre réponse éditée. Toujours le même problème. –

Questions connexes