2011-08-06 2 views
1

Я пытаюсь отобразить пользовательский TableView на UIViewController, но я получаю сообщение об ошибке «UITableView DataSource должен возвращать ячейку из Tableview: cellForRowAtIndexPath:»Пользовательские TableViewCells на UIViewController?

я подключен к TableView DataSource и делегат.

Любые предложения по внедрению так или мне нужен 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; 

ответ

2

ОШИБКА:

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

// в выше владелец должен самостоятельно

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

изменить эту строку

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

Извините, я не опубликовал весь код. Я отредактирую свой вопрос. –

+0

после редактирования скажите мне –

+0

Пила ваш отредактированный ответ. Все та же проблема. –

Смежные вопросы