2012-02-16 86 views
2

我想让UITableView看上去很像是它的样式为,但有一些细微差别(如不同的颜色和圆角较短的半径)。行具有可变的高度。我想为我的应用程序中的大多数表格视图控制器使用此自定义样式。查看Twitter,Groupon,& GitHub发布示例应用程序。自定义UITableViewCell backgroundView和selectedBackgroundView

我该怎么做?

我想这样做像做的。 套backgroundView & selectedBackgroundView既能的UIGroupTableViewCellBackground实例(的UIView的子类)。 UIGroupTableViewCellBackground是其层的委托并实现drawLayer:其层中的内容设定为CGImageRef

  1. 我敢肯定苹果根据Quartz 2D Programming Guide : Creating a Bitmap Graphics Context,也建议使用CGLayer考虑,而不是绘制成位图图形上下文创建此CGImageRef。这对于这个应用程序更好?

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

    下面是运行我的设备与选定的表视图“颜色混合层”的核芯显卡仪器工具的屏幕截图。你可以看到顶部的影子是混合的。而且,看起来最后一个单元格中还有一些混合。

    enter image description here

    enter image description here

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

    enter image description here

回答

0

你可以指望正在绘制的行数和图像设置为背景单个细胞。

,你所要做的就是

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

相关问题