2013-09-17 2 views
1

Я пытаюсь настроить UITableViewCell, но по какой-то причине он отображается как пустой. Есть ли что-то очевидное, я поступаю неправильно? Я вытащил menuLabel из раскадровки в качестве выхода, поэтому он привязан правильно, а ячейка в раскадровке связана с классом «MenuCell».Extended TableViewCell blank

В моем контроллере Tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"MenuCell"; 
[self.tableView registerClass:[MenuCell class] forCellReuseIdentifier:CellIdentifier]; 
MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if(cell == nil) { 
    NSLog(@"creating a new cell"); 
    cell = [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:@"MenuCell"]; 
} 

cell.menuLabel.text = @"Hello"; 


return cell; 
} 
+0

Почему добавление 'cell = (MenuCell *) [[[NSBundle mainBundle] loadNibNamed: @ "MenuCell" владелец: self options: nil] objectAtIndex: 0];' Работа? – Allen

+0

, потому что вам нужно загрузить nib для вашей пользовательской ячейки. – Bharat

+0

, но все, что у меня есть, есть в раскадровке, без файла nib? – Allen

ответ

2

Вы можете использовать этот

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

      MenuCell *cell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) 
      { 
       UINib* customCellNib = [UINib nibWithNibName:@"MenuCell" bundle:nil]; 

       // Register this nib file with cell identifier. 

       [tableView registerNib: customCellNib forCellReuseIdentifier:CellIdentifier]; 
       cell = (MenuCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      } 
    // Whatever you want 
retrun cell; 
    } 

Надеется, что это поможет. счастливое кодирование: P

1

Используйте этот один ....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"MenuCell"; 
     MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if (cell == nil) 
     { 
      cell = [[MenuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
      cell = (MenuCell *)[[[NSBundle mainBundle] loadNibNamed:@"MenuCell" owner:self options:nil] objectAtIndex:0]; 

     } 
     cell.menuLabel.text = @"Hello"; 
     return cell; 
    } 

Я надеюсь, это будет полезно.

+0

Почему это работало? странно, у меня есть тот же код, что и в другом tablewviewcontroller. htat точно так же, как и в проблеме, но он работает. – Allen

+0

это работает, но я не могу настроить внешний вид ячеек в загрузчике MenuCell – Allen

+0

, что я не понимаю? –