2016-05-12 89 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後,對不對?請注意'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; 
相關問題