2015-10-30 3 views
0

В моем представлении таблицы я пытаюсь использовать ячейку представления таблицы по умолчанию для большинства строк, но хочу использовать пользовательскую ячейку для одной строки. Проблема в том, что одна строка с пользовательской ячейкой. Я явно не загружаю его правильно, потому что я не могу получить доступ к свойствам пользовательской ячейки.Загрузка нескольких типов UITableViewCell

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; 

    [self.tableView registerNib:[UINib nibWithNibName:@"KeepLoginCell" bundle:nil] 
     forCellReuseIdentifier:@"KeepLoginCell"]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = nil; 

    cell.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 

    if (indexPath.section == 0) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

     if (!cell) { 
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
     } 

     if (indexPath.row == 0) { 
      cell.textLabel.text = @"Recent Purchases"; 
     } 
    } else if (indexPath.section == 1) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

     if (!cell) { 
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
     } 

     if (indexPath.row == 0) { 
      cell.textLabel.text = @"Payment Method"; 
     } 
    } else if (indexPath.section == 2) { 
     if (indexPath.row == 0) { 
      cell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"]; 

      if (!cell) { 
       cell = [[KeepLoginCell alloc]init]; 
      } 
     } else if (indexPath.row == 1) { 
      cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

      if (!cell) { 
       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
      } 

      cell.textLabel.text = [self.settingsArray objectAtIndex:indexPath.row]; 
     } 
    } 

    return cell; 
} 
+0

Заполнение пользовательских ячеек и возвратных ячеек в инструкции if. –

ответ

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];  
    if (!cell) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
    } 

    cell.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 

    if (indexPath.section == 0) {     
     if (indexPath.row == 0) { 
      cell.textLabel.text = @"Recent Purchases"; 
     } else { 
      //TODO: handle this case 
     } 
    } else if (indexPath.section == 1) {  
     if (indexPath.row == 0) { 
      cell.textLabel.text = @"Payment Method"; 
     } else { 
      //TODO: handle this case 
     } 
    } else if (indexPath.section == 2) { 
     if (indexPath.row == 0) { 
      KeepLoginCell *customCell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"]; 

      if (!customCell) { 
       customCell = [[KeepLoginCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KeepLoginCell"]; 
      } 
      //TODO: set values to your custom cell 
      //e.g. customCell.myLabel.text = ... 

      cell = customCell; 
     } else if (indexPath.row == 1) { 
      cell.textLabel.text = [self.settingsArray objectAtIndex:indexPath.row]; 
     } else { 
      //TODO: handle this case 
     } 
    } 

    return cell; 
} 
1

В этой строке:

cell = (KeepLoginCell *)[tableView dequeueReusableCellWithIdentifier:@"KeepLoginCell"]; 

Даже если вы бросили dequeue... сообщений для возвращать KeepLoginCell*, ваше значение cell еще объявлено как родовая UITableViewCell. Таким образом, подсказки редактора компилятора/статического анализатора/xcode показывают только то, что относится к UITableViewCell, а не к вашему пользовательскому подклассу.

Используйте вместо этого:

KeepLoginCell *klcell = (KeepLoginCell *)[tableView dequeue… 
[klcell setCustomProperty:…]; 
cell = klcell; 
0

Проблема заключается в том, что вы не имеете переменную типа KeepLoginCell в любом месте. Итак, где вы уверены, что ваша клетка является KeepLoginCell, вы должны либо бросить его при каждом вызове:

[((KeepLoginCell*)cell) keepIt]; 

или вы можете просто положить его в переменной этого типа

KeepLoginCell* keepLoginCell = (id)cell; 
[keepLoginCell keepIt]; 
Смежные вопросы