UITableView - 高亮选中的单元格 [iOS]

11

我创建了一个带有自定义单元格的UITableView,通常是按照这个教程进行的;

http://www.theappcodeblog.com/?p=353

我已经根据我的需求进行了自定义,并且它看起来非常好。 但是,当我选择一行时,它会通过将整个单元格变成蓝色来突出显示该行。 这个高亮覆盖了整个单元格并覆盖了内容,实际上制作了一个难看的大蓝框。 我尝试了备选高亮选项:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.selectionStyle = UITableViewCellSelectionStyleGray;

显然,如果选择灰色,则会出现相同的行为,但是带有一个灰色的丑陋框。目前,“None”选项是理想的选择,但对我的用户来说并不提供良好的体验,因为我想给出一些指示,表明他们已经选择了该行。

有人能建议一种方法来显示该行已被突出显示,但仍显示其下面的文本吗?使用半透明度?

谢谢

3个回答

11
也许是因为您的示例代码设置了:artistLabel.highlightedTextColor = [UIColor clearColor];。当高亮时,它会导致artistLabel文本颜色变为透明。
当然,您可以将自己的颜色设置为标签的highlightedTextColor属性,例如:[UIColor whiteColor]

我真是个绝对的白痴,谢谢。简单明了的答案,完美解决了问题。谢谢。 - Paul Morris
1
不客气。如果您认为这个答案有用,投一票并选择它作为正确答案将非常感激。 - xuzhe
我总是这样做。我对自己提出的每个问题都有100%的把握。再次感谢。 - Paul Morris
谢谢你,也感谢你。保持100%的速率很难,这真是太不可思议了。 - xuzhe

8

UITableView的选中颜色

享受...

// Image
UIImage *selectionBackground = [UIImage imageNamed:@"g7.png"];
UIImageView *bgview=[[UIImageView alloc] initWithImage:selectionBackground];
cell.selectedBackgroundView=bgview;
[bgview release];

或者

// Color
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor brownColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];

我也建议调用[cell setSelectionStyle: UITableViewCellSelectionStyleNone],这可以防止当选中单元格时在其周围出现白色矩形。 - Nikolay Shubenkov

5

您可以使用自定义视图进行选择。

这里有一个绿色的视图,你可以根据需要进行任何自定义更改。

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero 
                                   reuseIdentifier: CellIdentifier] autorelease];


    UIView *selectionView = [[UIView alloc]initWithFrame:cell.bounds];

    [selectionView setBackgroundColor:[UIColor greenColor]];

    cell.selectedBackgroundView = selectionView;


    [selectionView release];

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接