自定义UITableViewCell将恢复为selectRowAtIndexPath的标准蓝色单元格

时间:2013-09-09 05:13:12

标签: ios objective-c uitableview

我有UITableViewCell子类,它为我提供了自定义样式和额外数据。

当用户进行互动时,一切正常,但是当我通过调用selectRowAtIndexPath以编程方式选择它时,它会恢复为蓝色背景的标准选定UITableViewCell

这是我的代码

[myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

5 个答案:

答案 0 :(得分:1)

请参阅此问题:UITableView Cell selected Color

作为解决方法,您可以覆盖didSelectRowAtIndexPath并在那里设置背景颜色:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];

backgroungColor设置为普通颜色覆盖didDeselectRowAtIndexPath

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

答案 1 :(得分:0)

如果您已实施didSelectRowAtIndexPath,则在致电:

之后
[myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

添加以下行:

[[myTable delegate]tableView:myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

写:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

答案 2 :(得分:0)

cellForRowAtIndexPath方法中,在返回单元格实例之前添加此行:

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

答案 3 :(得分:0)

从@Andrey Gordeev建议的解决方法开始,我将其扩展为包括在取消选择单元格后删除样式层。

由于其他原因,我仍然需要使用我的自定义单元格,但我已将其显示为下面的标准单元格,以便任何人都可以使用它

如果您必须致电

,请应用自己的风格
[myTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];

选择单元格

时添加渐变图层
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    CAGradientLayer *gradient = [CAGradientLayer layer];
    [gradient setFrame:[cell.contentView bounds]];
    [gradient setColors:[NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.70 alpha:1.0] CGColor], (id)[[UIColor colorWithWhite:0.50 alpha:1.0] CGColor], nil]];
    [[cell.contentView layer] insertSublayer:gradient atIndex:0];
    // The next line makes a reference to the gradient layer for selection and removal later on
    [[cell.contentView layer] setValue:gradient forKey:@"GradientLayer"];

    ...

}

取消选择单元格

时删除渐变图层
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    // Get the layer using the reference to it
    CALayer *gradientLayer = [[cell.contentView layer] valueForKey:@"GradientLayer"];
    [gradientLayer removeFromSuperlayer];
    [[cell.contentView layer] setValue:nil forKey:@"GradientLayer"];

    ...

}

答案 4 :(得分:0)

我有类似的问题,这是我如何解决它:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_NAME];...

      // Cell custom style
UIImage *background             = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image        = background;
cell.backgroundView             = cellBackgroundView;

UIImage *selectedBackground             = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellselectedBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image                = selectedBackground;
cell.selectedBackgroundView             = cellselectedBackgroundView;