【问题标题】:Custom UITableViewCell backgroundView & selectedBackgroundView自定义 UITableViewCell backgroundView & selectedBackgroundView
【发布时间】:2023-03-05 06:27:01
【问题描述】:

我想设置UITableView 的样式,让它看起来很像UITableViewStyleGrouped 的样式,但有一些细微的差别(比如不同的颜色和半径更短的圆角)。行具有可变高度。我想为我的应用程序中的大多数表格视图控制器使用这种自定义样式。有关示例,请参阅 Twitter、Groupon 和 GitHub 问题应用。

我应该怎么做?

我正在考虑像UITableViewStyleGrouped 那样做。 UITableViewStyleGroupedbackgroundViewselectedBackgroundView 都设置为 UIGroupTableViewCellBackground 的实例(UIView 的子类)。 UIGroupTableViewCellBackground 是其层的代表,并实现drawLayer: 以将其层的内容设置为CGImageRef

  1. 我很确定 Apple 根据Quartz 2D Programming Guide : Creating a Bitmap Graphics Context 创建了这个CGImageRef,这也建议考虑使用CGLayer 而不是绘制到位图图形上下文。哪个更适合这个应用程序?

  2. 另外,UITableViewStyleGrouped 表格视图的第一个单元格使用UIImageView 在其顶部添加了一个带圆角的阴影(它将其设置为可调整大小(宽度方向)UIImage,带有一个半透明的 PNG 文件。 ) 为什么这样做?这不是滚动缓慢吗?为什么不将其绘制到第一个单元格的 CGImageRef 上?也许性能的下降并不显着,并且更容易让单元格在图像中看起来正确。我将单元格的CGImageRef 保存到磁盘并使用预览打开它。它仍然有圆角。这个覆盖视图只是添加了顶部阴影。

    下面是在我的设备上运行 Core Graphics Instruments 工具的屏幕截图,其中为 UITableViewStyleGrouped 表格视图选择了“颜色混合层”。您可以看到顶部的阴影是混合的。而且,看起来最后一个单元格中也发生了一些混合。

    选择第三个单元格(不混合)。选择第一个或最后一个单元格的行为相同,但不会摆脱已经混合的叠加视图。

【问题讨论】:

    标签: uitableview uikit


    【解决方案1】:

    您可以计算正在绘制的行数并将图像设置为单个单元格的背景。

    你要做的是

    cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
    cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
    

    使用此方法设置背景图片

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
    {      
    
          &&&&
    
    
         if((indexPath.row)==0)  
             // set image upper corner image here
    
         else if (indexPath.row==1)  
            //set normal image here
    
         else if (indexPath.row==YourArray)
            // also set the lower corner round image in the last low, which can be calculated by comparing indexpath.row to the mutablearray/array
    
    }
    

    您还可以在 &&& 部分之前应用一个条件来应用不同的图像,如果 array.count == 1 则上下角都是圆形的

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      相关资源
      最近更新 更多