自定义 UITableViewCell backgroundView &选择的背景视图 [英] Custom UITableViewCell backgroundView & selectedBackgroundView

查看:23
本文介绍了自定义 UITableViewCell backgroundView &选择的背景视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置 UITableView 的样式,使其看起来很像 UITableViewStyleGrouped 的样式,但有一些细微的差异(例如不同的颜色和半径较短的圆角).行具有可变的高度.我想对我的应用程序中的大多数表视图控制器使用这种自定义样式.参见 Twitter、Groupon 和GitHub Issues 应用程序示例.

I want to style a UITableView to look a lot like if it had the style UITableViewStyleGrouped but with some subtle differences (like different colors and rounded corners with a shorter radius). The rows have variable height. I want to use this custom style for most table view controllers in my app. See the Twitter, Groupon, & GitHub Issues apps for examples.

我该怎么做?

我正在考虑像 UITableViewStyleGrouped 那样做.UITableViewStyleGrouped 设置 backgroundView &selectedBackgroundView 都是 UIGroupTableViewCellBackground 的实例(UIView 的子类).UIGroupTableViewCellBackground 是其层的委托,并实现了 drawLayer: 以将其层的内容设置为 CGImageRef.

I'm thinking of doing like UITableViewStyleGrouped does. UITableViewStyleGrouped sets backgroundView & selectedBackgroundView both to instances of UIGroupTableViewCellBackground (a subclass of UIView). UIGroupTableViewCellBackground is a delegate of its layer and implements drawLayer: to set its layer's contents to a CGImageRef.

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

  1. I'm pretty sure Apple creates this CGImageRef according to Quartz 2D Programming Guide : Creating a Bitmap Graphics Context, which also suggests considering using CGLayer instead of drawing to a bitmap graphics context. Which is better for this application?

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

Also, the first cell of a UITableViewStyleGrouped table view adds a shadow with rounded-corners to its top using UIImageView (It sets it to a resizable (width-wise) UIImage with a translucent PNG file.) Why does it do that? Doesn't this slow scrolling? Why doesn't it just draw this to the CGImageRef for the first cell? Maybe the decrease in performance wasn't significant and it was easier to get the cell to look correct with an image. I saved the cell's CGImageRef to disk and opened it with Preview. It still has rounded corners. This overlaid view just adds the top shadow.

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

Below is a screenshot of running the Core Graphics Instruments tool on my device with "Color Blended Layers" selected for a UITableViewStyleGrouped table view. You can see that the shadow at the top is blended. And, it looks like there's some blending going on in the last cell too.

选择第三个单元格(无混合).选择第一个或最后一个单元格的作用相同,但不会去除已经混合的叠加视图.

With the third cell selected (no blending). Selecting the first or last cell acts the same but doesn't get rid of the already blended overlaid views.

推荐答案

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

you can count the number of rows being drawn and set the image as background for individual cell.

你要做的是

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

使用此方法设置背景图片

use this method to set the background image

- (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 的情况下应用不同的图像,该图像的上角和下角都是圆形的

You can also apply one condition in before the &&& part to apply a different image which has both upper and lower corners round in case array.count == 1

这篇关于自定义 UITableViewCell backgroundView &选择的背景视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆