【问题标题】:Custom UITableViewCell reverts to standard blue cell for selectRowAtIndexPath自定义 UITableViewCell 恢复为 selectRowAtIndexPath 的标准蓝色单元格
【发布时间】:2013-09-09 05:13:12
【问题描述】:

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

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

这是我的代码

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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案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  
    

    【讨论】:

    • 现在就试试这个,但是当它被取消选择时它不会删除那个背景颜色。
    【解决方案2】:

    如果你已经实现了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;
    

    【讨论】:

    • 我已经这样做了,但它对所选样式没有影响
    • 删除了所有样式
    • 如果你自己做了一些自定义样式,它应该覆盖默认
    • @PaulMrG 只需使用cell.selectionStyle = UITableViewCellSelectionStyleNone;。不要将 selectedBackground 设置为 nil。现在会发生什么?
    • 不知道您要使用哪种方法,因此尝试了 cellForRowAtIndexPath 和 didSelectRowAtIndexPath (分别),其中任何一个都删除了所有样式。
    【解决方案3】:

    cellForRowAtIndexPath 方法中,在返回您的单元格实例之前添加这一行:

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    

    【讨论】:

    • 与我在@anum90 中尝试过的相同,它删除了所有样式而不是使用我的自定义样式
    【解决方案4】:

    从@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"];
    
        ...
    
    }
    

    【讨论】:

      【解决方案5】:

      我遇到了类似的问题,我是这样解决的:

      - (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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-16
        • 1970-01-01
        • 2016-06-13
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多