UITableView cell textLabel color

前端 未结 3 1732
慢半拍i
慢半拍i 2020-12-29 09:07

I have a simple issue with UITableViewCell. What I want is to change the text color of a selected cell. In my cellForRowAtIndexPath method, I set:<

相关标签:
3条回答
  • 2020-12-29 09:13

    Got the solution by setting color in if (cell == nil) check

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.textLabel.textColor = [UIColor whiteColor];  
    }     
    

    and

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
        [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];
    
    }
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
            [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];
    
    }
    

    Thanks

    0 讨论(0)
  • 2020-12-29 09:17

    set a global : int m_selectedCell; modify it in

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    and

    just add

    if(m_selectedCell == indexPath.row){
    ...
    ...
    }else{
    }
    

    in

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
    

    that's all !

    0 讨论(0)
  • 2020-12-29 09:37

    All that you have to do is

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.highlightedTextColor = [UIColor orangeColor];
    

    inside if(cell == nil)

    that will work fine.

    0 讨论(0)
提交回复
热议问题