UITableView单元格选择颜色?

本文翻译自:UITableView Cell selected Color?

I have created a custom UITableViewCell . 我创建了一个自定义UITableViewCell The table view is showing data fine. 表格视图显示的数据很好。 What I am stuck in is when user touches cell of tableview, then I want to show the background color of the cell other than the default [blue color] values for highlighting the selection of cell. 我遇到的问题是,当用户触摸表格视图的单元格时,我想显示该单元格的背景色,而不是默认的[蓝色]值,以突出显示该单元格的选择。 I use this code but nothing happens: 我使用此代码,但没有任何反应:

cell.selectedBackgroundView.backgroundColor=[UIColor blackColor];

#1楼

参考:https://stackoom.com/question/8NyJ/UITableView单元格选择颜色


#2楼

One more tip to Christian's way to show rounded corner background for grouped table. 克里斯蒂安(Christian)为分组表显示圆角背景的方法的另一个提示。

If I use cornerRadius = 10 for cell, it shows four corner's rounded selection background. 如果我对单元格使用cornerRadius = 10 ,它将显示四个角的四舍五入选择背景。 It's not the same with table view's default UI. 这与表格视图的默认用户界面不同。

So, I think about easy way to resolve it with cornerRadius . 因此,我想到了使用cornerRadius解决此问题的简便方法。 As you can see from the below codes, check about cell's location (top, bottom, middle or topbottom) and add one more sub layers to hide top corner or bottom corner. 从下面的代码中可以看到,检查单元格的位置(顶部,底部,中间或顶部底部),然后再添加一个子层以隐藏顶部角或底部角。 This just shows exactly same look with default table view's selection background. 这仅显示与默认表视图的选择背景完全相同的外观。

I tested this code with iPad splitterview . 我使用iPad splitterview测试了此代码。 You can change patchLayer's frame position as you needed. 您可以根据需要更改patchLayer的框架位置。

Please let me know if there is more easier way to achieve same result. 请让我知道是否有更简单的方法来达到相同的结果。

if (tableView.style == UITableViewStyleGrouped) 
{
    if (indexPath.row == 0) 
    {
        cellPosition = CellGroupPositionAtTop;
    }    
    else 
    {
        cellPosition = CellGroupPositionAtMiddle;
    }

    NSInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
    if (indexPath.row == numberOfRows - 1) 
    {
        if (cellPosition == CellGroupPositionAtTop) 
        {
            cellPosition = CellGroupPositionAtTopAndBottom;
        } 
        else 
        {
            cellPosition = CellGroupPositionAtBottom;
        }
    }

    if (cellPosition != CellGroupPositionAtMiddle) 
    {
        bgColorView.layer.cornerRadius = 10;
        CALayer *patchLayer;
        if (cellPosition == CellGroupPositionAtTop) 
        {
            patchLayer = [CALayer layer];
            patchLayer.frame = CGRectMake(0, 10, 302, 35);
            patchLayer.backgroundColor = YOUR_BACKGROUND_COLOR;
            [bgColorView.layer addSublayer:patchLayer];
        } 
        else if (cellPosition == CellGroupPositionAtBottom) 
        {
            patchLayer = [CALayer layer];
            patchLayer.frame = CGRectMake(0, 0, 302, 35);
            patchLayer.backgroundColor = YOUR_BACKGROUND_COLOR;
            [bgColorView.layer addSublayer:patchLayer];
        }
    }
}

#3楼

Here is the important parts of the code needed for a grouped table. 这是分组表所需的代码的重要部分。 When any of the cells in a section are selected the first row changes color. 当选择一个节中的任何单元格时,第一行会更改颜色。 Without initially setting the cellselectionstyle to none there is an annonying double reload when the user clicks row0 where the cell changes to bgColorView then fades and reloads bgColorView again. 最初没有将cellslectionstyle设置为none的情况下,当用户单击row0时,会出现一个令人讨厌的两次重新加载,其中单元格更改为bgColorView,然后淡出并再次重新加载bgColorView。 Good Luck and let me know if there is a simpler way to do this. 祝您好运,如果有更简单的方法,请告诉我。

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if ([indexPath row] == 0) 
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.layer.cornerRadius = 7;
        bgColorView.layer.masksToBounds = YES;
        [bgColorView setBackgroundColor:[UIColor colorWithRed:.85 green:0 blue:0 alpha:1]];
        [cell setSelectedBackgroundView:bgColorView];

        UIColor *backColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
        cell.backgroundColor = backColor;
        UIColor *foreColor = [UIColor colorWithWhite:1 alpha:1];
        cell.textLabel.textColor = foreColor;

        cell.textLabel.text = @"row0";
    }
    else if ([indexPath row] == 1) 
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UIColor *backColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        cell.backgroundColor = backColor;
        UIColor *foreColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        cell.textLabel.textColor = foreColor;

        cell.textLabel.text = @"row1";
    }
    else if ([indexPath row] == 2) 
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UIColor *backColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        cell.backgroundColor = backColor;
        UIColor *foreColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        cell.textLabel.textColor = foreColor;

        cell.textLabel.text = @"row2";
    }
    return cell;
}

#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

    [tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tvStat cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

#pragma mark Table view Gestures

-(IBAction)singleTapFrom:(UIGestureRecognizer *)tapRecog
{

    CGPoint tapLoc = [tapRecog locationInView:tvStat];
    NSIndexPath *tapPath = [tvStat indexPathForRowAtPoint:tapLoc];

    NSIndexPath *seleRow = [tvStat indexPathForSelectedRow];
    if([seleRow section] != [tapPath section])
        [self tableView:tvStat didDeselectRowAtIndexPath:seleRow];
    else if (seleRow == nil )
        {}
    else if([seleRow section] == [tapPath section] || [seleRow length] != 0)
        return;

    if(!tapPath)
        [self.view endEditing:YES];

    [self tableView:tvStat didSelectRowAtIndexPath:tapPath];
}

#4楼

It's easy when the table view style is plain, but in group style, it's a little trouble, I solve it by: 表格视图样式简单时很容易,但是在组样式下比较麻烦,我可以通过以下方法解决:

CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kGroupTableViewCellWidth+2, cellHeight)];
view.backgroundColor = kCommonHighlightedColor;
cell.selectedBackgroundView = view;
[view release];
UIRectCorner cornerFlag = 0;
CGSize radii = CGSizeMake(0, 0);
NSInteger theLastRow = --> (yourDataSourceArray.count - 1);
if (indexPath.row == 0) {
    cornerFlag = UIRectCornerTopLeft | UIRectCornerTopRight;
    radii = CGSizeMake(10, 10);
} else if (indexPath.row == theLastRow) {
    cornerFlag = UIRectCornerBottomLeft | UIRectCornerBottomRight;
    radii = CGSizeMake(10, 10);
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:cornerFlag cornerRadii:radii];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = maskPath.CGPath;
view.layer.mask = shapeLayer;

noted the kGroupTableViewCellWidth, I define it as 300, it's the width of group table view cell width in iPhone 注意kGroupTableViewCellWidth,我将其定义为300,它是iPhone中组表视图单元格宽度的宽度


#5楼

I think you were on the right track, but according to the class definition for selectedBackgroundView : 我认为您走在正确的轨道上,但是根据selectedBackgroundView的类定义:

The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for section-group tables UITableViewStyleGrouped). 对于普通样式表(UITableViewStylePlain)中的单元,默认值为nil;对于节组表UITableViewStyleGrouped,默认值为nil。

Therefore, if you're using a plain-style table, then you'll need to alloc-init a new UIView having your desired background colour and then assign it to selectedBackgroundView . 因此,如果您使用的是普通样式表,则需要分配-初始化一个具有所需背景色的新UIView ,然后将其分配给selectedBackgroundView

Alternatively, you could use: 或者,您可以使用:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

if all you wanted was a gray background when the cell is selected. 如果选中该单元格时您想要的只是一个灰色背景。 Hope this helps. 希望这可以帮助。


#6楼

[cell setSelectionStyle:UITableViewCellSelectionStyleGray];

确保您已使用以上行来使用选择效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值