2013-12-21 48 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

然後,您需要在您的UITableViewDelegate實施tableView:shouldHighlightRowAtIndexPath:

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

謝謝您的回覆。但是當我使用上面的代碼時,突出顯示的文本顏色不顯示? – Jeff

相關問題