2016-05-12 2 views
0

Я не могу понять, как добавить текст в другой цвет в правую часть ячейки таблицы. Вот код, который я добавил.Добавить правильную деталь в ячейку для просмотра таблицы

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:informationTableIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:informationTableIdentifier]; 
} 

[cell.textLabel setFont:[UIFont systemFontOfSize:15]]; 
[cell.textLabel setTextColor:[UIColor blackColor]]; 


if (indexPath.section == 0) { 
    cell.textLabel.text = [infoCellFirstSection objectAtIndex:indexPath.row]; 
} 
if (indexPath.section == 1) { 
    if (indexPath.row == 3 || indexPath.row == 3) { 
     cell.detailTextLabel.text = @"@SykesHarvey"; 
     [cell.detailTextLabel setTextColor:[UIColor colorWithRed:0.48 green:0.93 blue:0.25 alpha:1.0]]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:informationTableIdentifier]; 
    } 
    cell.textLabel.text = [infoCellSecondSection objectAtIndex:indexPath.row]; 
} 
if (indexPath.section == 2) { 
    if (indexPath.row == 2 || indexPath.row == 3) { 
     [cell.textLabel setFont:[UIFont systemFontOfSize:14]]; 
     [cell.textLabel setTextColor:[UIColor grayColor]]; 
     cell.textLabel.numberOfLines = 0; 
     cell.backgroundColor = [UIColor clearColor]; 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
    } 
    cell.textLabel.text = [infoCellThirdSection objectAtIndex:indexPath.row]; 
} 
if (indexPath.section == 3) { 
    cell.textLabel.text = [infoCellFourthSection objectAtIndex:indexPath.row]; 
} 
if (indexPath.section == 4) { 
    cell.textLabel.text = [infoCellFifthSection objectAtIndex:indexPath.row]; 
} 
return cell; 
} 

Все это представляет это enter image description here, но я хочу сказать, @SykesHarvey на правой стороне ячейки для разработчиков. Пожалуйста, кто-нибудь может помочь?

+0

Я не понимаю, что вы делаете: В вашем случае с вопросом, вы делаете сразу после того, вы пытались установить detailsText вы делаете целый новый Alloc/инициализации для ячейки? Обратите внимание: 'cell.detailsText' должен быть' nil', потому что нет 'detailsText' с' UITableViewCellStyleDefault' – Larme

+0

Можете ли вы объяснить подробно? Именно в чем проблема? –

ответ

1

Вы сброс клетки detailTextLabel на повторной инициализации его в следующей строке. Я немного оптимизировал ваш код. Попробуйте это:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:informationTableIdentifier]; 

    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:informationTableIdentifier]; 
    } 
    [cell.textLabel setFont:[UIFont systemFontOfSize:15]]; 
    [cell.textLabel setTextColor:[UIColor blackColor]]; 

    switch (indexPath.section) { 
    case 0: 
     cell.textLabel.text = [infoCellFirstSection objectAtIndex:indexPath.row]; 

     break; 
    case 1: 
     if (indexPath.row == 3 || indexPath.row == 3) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:informationTableIdentifier]; 
      [cell.textLabel setFont:[UIFont systemFontOfSize:15]]; 
      [cell.textLabel setTextColor:[UIColor blackColor]]; 

      [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
      cell.detailTextLabel.text = @"@SykesHarvey"; 
      [cell.detailTextLabel setTextColor:[UIColor colorWithRed:0.48 green:0.93 blue:0.25 alpha:1.0]]; 
     } 
     cell.textLabel.text = [infoCellSecondSection objectAtIndex:indexPath.row]; 

     break; 
    case 2: 
     if (indexPath.row == 2 || indexPath.row == 3) { 
      [cell.textLabel setFont:[UIFont systemFontOfSize:14]]; 
      [cell setAccessoryType:UITableViewCellAccessoryNone]; 
      [cell.textLabel setTextColor:[UIColor grayColor]]; 
      cell.textLabel.numberOfLines = 0; 
      cell.backgroundColor = [UIColor clearColor]; 
     } 
     cell.textLabel.text = [infoCellThirdSection objectAtIndex:indexPath.row]; 

     break; 
    case 3: 
     cell.textLabel.text = [infoCellFourthSection objectAtIndex:indexPath.row]; 

     break; 
    case 4: 
     cell.textLabel.text = [infoCellFifthSection objectAtIndex:indexPath.row]; 

     break; 
    default: 
     break; 
    } 

    return cell; 
} 
0

Попробуйте это:

cell.detailTextLabel.textAlignment = NSTextAlignmentRight; 
Смежные вопросы