UITableView样式分组中的自定义selectedBackgroundView出站

RAG*_*poR 2 iphone uitableview ios

在此输入图像描述 使用UITableView Style Grouped后,使用带自定义颜色的selectedBackgroundView时会出现问题.

它在那里画UITableViewCell.是否有可能将它们绑定?

小智 5

我有类似的问题,没有找到任何简单易行的方法来解决.我为UIImage制作了类别,并使用了几个图像来捕捉所有情况.

  • single_cell_bg.png - 包含所有圆角的图像
  • top_cell_bg - 带圆角顶角的图像
  • .....

不那么优雅但工作

@interface UIImage (CellBacground)
- (UIImage *)backgroundCellViewforRow:(NSInteger)row totalRow:(NSInteger)total;
@end



#import "UIImage+CellBacground.h"

@implementation UIImage (CellBacground)

- (UIImage *)backgroundCellViewforRow:(NSInteger)row totalRow:(NSInteger)total {
    NSString *path = NULL;
    if (row == 0) {
        if(total == 1) {
            path = [[NSBundle mainBundle] pathForResource:@"single_cell_bg" ofType:@"png"];
        } else {
            path = [[NSBundle mainBundle] pathForResource:@"top_cell_bg" ofType:@"png"];
        }
    } else {
        if ((total - row) == 1) {
            path = [[NSBundle mainBundle] pathForResource:@"bottom_cell_bg" ofType:@"png"];
        } else {
            path = [[NSBundle mainBundle] pathForResource:@"middle_cell_bg" ofType:@"png"];
        }
    }
    UIImage *theImage = [[UIImage alloc] initWithContentsOfFile:path];
    UIEdgeInsets imInset = UIEdgeInsetsMake(10, 10, 10, 10);
    UIImage *strImage = [theImage resizableImageWithCapInsets:imInset];
    return strImage;
}
@end
Run Code Online (Sandbox Code Playgroud)

在tableView中调用:cellForRowAtIndexPath:

UIImage *backImage = [[UIImage alloc] init];
[backImage backgroundCellViewforRow:indexPath.row totalRow:[tableView numberOfRowsInSection:indexPath.section]];
UIImageView *backview = [[UIImageView alloc] initWithImage:backImage];
cell.selectedBackgroundView = backview;
Run Code Online (Sandbox Code Playgroud)