2013-12-21 3 views
0

В моем моем приложении я хочу изменить цвет текста ячейки с вне исчезающие клеток separator.And Я использую следующий кодКак изменить цвет текста ячейки без скрытия разделителя?

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

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 

     UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; 
     [selectedBackgroundView setBackgroundColor:[UIColor clearColor]]; // set color here 
     [cell setSelectedBackgroundView:selectedBackgroundView]; 

     cell.backgroundColor=[UIColor clearColor]; 
     cell.textLabel.highlightedTextColor = [UIColor redColor]; 
     [cell setOpaque:NO]; 

    } 
    cell.textLabel.text=[contentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

Но когда я нажал на ячейку сепаратор клеток также исчезает? Как изменить цвет текста, не скрывая разделитель?

+1

Извините, но ваш вопрос не ясно, что я имею в виду, как и когда вы хотите изменить текст цвет? – Retro

+0

@Retro Я хочу изменить цвет текста при нажатии на ячейку – Jeff

+0

Установить 'tableView.separatorStyle = UITableViewCellSeparatorStyleNone;' Добавить пользовательскую строку в ячейке – NANNAV

ответ

1

Кажется, клеточные сепараторы являются проблемой для многих людей. Так что, я бы сказал, а не делать то, что я предложил, чтобы отключить выбор, было бы проще установить сепаратор клеток к «ни» и управлять отделитель самостоятельно в фоновом режиме и отдельные взгляды фона:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // ... 

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
} 

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

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 

     UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; 
     [selectedBackgroundView setBackgroundColor:[UIColor clearColor]]; 
     [cell setSelectedBackgroundView:selectedBackgroundView]; 

     UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame]; 
     [backgroundView setBackgroundColor:[UIColor clearColor]]; 
     [cell setBackgroundView:backgroundView]; 

     UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, cell.frame.size.height - 1, cell.frame.size.width - tableView.separatorInset.left, 1)]; 
     UIView *backgroundSeparator = [[UIView alloc] initWithFrame:selectedBackgroundSeparator.frame]; 

     selectedBackgroundSeparator.backgroundColor = backgroundSeparator.backgroundColor = tableView.separatorColor; 

     [selectedBackgroundView addSubview:selectedBackgroundSeparator]; 
     [backgroundView addSubview:backgroundSeparator]; 

     cell.textLabel.highlightedTextColor = [UIColor redColor]; 
     [cell setOpaque:NO]; 

    } 
    cell.textLabel.text=[contentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

в качестве альтернативы, вы можете использовать разделитель сот по умолчанию, и вместо того, чтобы просто добавить свой собственные верхние и нижние разделители к selectedBackgroundView:

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

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 

     UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame]; 
     [selectedBackgroundView setBackgroundColor:[UIColor clearColor]]; 
     [cell setSelectedBackgroundView:selectedBackgroundView]; 

     UIView *topSelectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, 0, cell.frame.size.width - tableView.separatorInset.left, 1)]; 
     UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectOffset(topSelectedBackgroundSeparator.frame, 0, cell.frame.size.height)]; 

     topSelectedBackgroundSeparator.backgroundColor = selectedBackgroundSeparator.backgroundColor = tableView.separatorColor; 

     [selectedBackgroundView addSubview:selectedBackgroundSeparator]; 
     [selectedBackgroundView addSubview:topSelectedBackgroundSeparator]; 

     cell.textLabel.highlightedTextColor = [UIColor redColor]; 
     [cell setOpaque:NO]; 

    } 
    cell.textLabel.text=[contentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 
0
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    for(id view in cell.containtView.subview) { 
    if([view isKindaOfClass:[UILabel class]]) 
     UILabel* titleLabel = (UILabel*)view; 
     [titleLabel setTextColor:[UIColor whiteColor]]; // any you want 
    } 
} 
0

Вместо установки клетки selectedBackgroundView к четкому мнению, просто не позволяет клетке быть выделены при выборе выполнить то, что вы хотите? Это предотвратит автоматическое изменение ячейки и разделителей на основе выбора, но вам придется управлять распознаванием жестов кран и выделять текст ярлыка самостоятельно.

Извлеките selectedBackgroundView из вашего кода.

Затем нужно реализовать tableView:shouldHighlightRowAtIndexPath: в вашем UITableViewDelegate:

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return NO; // do what's appropriate based on the indexPath 
} 
+0

Благодарим вас за ответ. Но когда я использую вышеуказанный код, выделенный цвет текста не отображается? – Jeff

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