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

Iphone 以UITableView样式分组的自定义selectedBackgroundView出站,iphone,ios,uitableview,Iphone,Ios,Uitableview,使用UITableView样式分组后,使用自定义颜色的selectedBackgroundView时出现问题 它在UITableViewCell的外部绘制。 是否可以在绑定中剪裁它们?

使用UITableView样式分组后,使用自定义颜色的selectedBackgroundView时出现问题

它在UITableViewCell的外部绘制。
是否可以在绑定中剪裁它们?

myCellView.backgroundColor=[uicolorWithred:1绿:1蓝:0.75α:1]

cell.selectedBackgroundView=myCellView


[菌丝体视图发布]

我也有类似的问题,没有找到任何简单易行的方法来解决。我为UIImage创建了一个类别,并使用了多个图像来捕获所有案例

  • 单个单元格\u bg.png-具有所有圆角的图像
  • 顶部单元格背景-顶部圆角的图像
不是很优雅,但很实用

@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
在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;

这里有一个真正的解决方案,它不使用图像,并且在任何情况下都应该有效。这有点棘手,因为仅iPad上的tableView:willDisplayCell:ForRowatineXpath:存在问题(请参阅评论)。除此之外,一旦你知道如何绕过各个角落,事情就会变得相当自然

控制器必须是圆角的控制器,因为它知道单元格的位置(第一个、最后一个等),并影响圆角。进行舍入的最佳位置似乎是tableView:willDisplayCell:forRowAtIndexPath:。以下是UITableViewController子类的示例:

#import "MainViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface CustomCell : UITableViewCell

@end

@implementation CustomCell

- (void)installSelectedBackgroundView {
    UIView *view = [[UIView alloc] initWithFrame:self.frame];
    view.backgroundColor = [UIColor redColor]; // choose a color or add a gradient, etc
    view.layer.borderWidth = 1; // optional border
    view.layer.borderColor = [[UIColor grayColor] CGColor];
    self.selectedBackgroundView = view;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self installSelectedBackgroundView];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self installSelectedBackgroundView];
}

@end

@implementation MainViewController

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
    return cell;
}

+ (void)roundCorners:(UIRectCorner)corners forView:(UIView *)view {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

+ (void)roundCornersForSelectedBackgroundViewForTableView:(UITableView *)tableView cell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    NSUInteger rows = [tableView numberOfRowsInSection:indexPath.section];
    if (rows == 1) {
        [[self class] roundCorners:UIRectCornerAllCorners forView:cell.selectedBackgroundView];
    } else if (indexPath.row == 0) {
        [[self class] roundCorners:UIRectCornerTopLeft | UIRectCornerTopRight forView:cell.selectedBackgroundView];
    } else if (indexPath.row == rows-1) {
        [[self class] roundCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight forView:cell.selectedBackgroundView];
    } else {
        [[self class] roundCorners:0 forView:cell.selectedBackgroundView];
    }
}

+ (void)deferredRoundCornersForSelectedBackgroundViewForTableView:(UITableView *)tableView cell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    int64_t delayInSeconds = 0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [[self class] roundCornersForSelectedBackgroundViewForTableView:tableView cell:cell indexPath:indexPath];
    });
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // cell.selectedBackgroundView.frame is not sized correctly yet, so rounding the corners has to be deferred briefly
    [[self class] deferredRoundCornersForSelectedBackgroundViewForTableView:tableView cell:cell indexPath:indexPath];
}

@end
#导入“MainViewController.h”
#进口
@接口CustomCell:UITableViewCell
@结束
@实现自定义单元
-(无效)InstalledSelectedBackgroundView{
UIView*view=[[UIView alloc]initWithFrame:self.frame];
view.backgroundColor=[UIColor redColor];//选择颜色或添加渐变,等等
view.layer.borderWidth=1;//可选边框
view.layer.borderColor=[[UIColor grayColor]CGColor];
self.selectedBackgroundView=视图;
}
-(id)initWithStyle:(UITableViewCellStyle)样式重用标识符:(NSString*)重用标识符{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我){
[自行安装的SelectedBackgroundView];
}
回归自我;
}
-(无效)从NIB中唤醒{
[超级awakeFromNib];
[自行安装的SelectedBackgroundView];
}
@结束
@实现主视图控制器
#pragma标记-表视图数据源
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回3;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
CustomCell*cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
cell.textLabel.text=[NSString stringWithFormat:@“cell%d”,indexPath.row];
返回单元;
}
+(无效)圆角:(UIRectCorner)视图的角:(UIView*)视图{
UIBezierPath*maskPath=[UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:角点
角半径:CGSizeMake(10.0,10.0)];
CAShapeLayer*maskLayer=[CAShapeLayer层];
maskLayer.frame=view.bounds;
maskLayer.path=maskPath.CGPath;
view.layer.mask=maskLayer;
}
+(void)roundCornersForSelectedBackgroundViewForTableView:(UITableView*)表格视图单元格:(UITableViewCell*)单元格indexPath:(NSIndexPath*)indexPath{
NSUTEGER rows=[tableView numberOfRowsInSection:indexPath.section];
如果(行==1){
[[self class]roundCorners:UIRectCornerAllCorners for View:cell.selectedBackgroundView];
}else if(indexPath.row==0){
[[self class]roundCorners:UIRectCornerTopLeft | UIRectCornerTopRight forView:cell.selectedBackgroundView];
}else if(indexPath.row==rows-1){
[[self class]roundCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight forView:cell.selectedBackgroundView];
}否则{
[[self class]圆角:0 forView:cell.selectedBackgroundView];
}
}
+(void)DelferredRoundCornersForSelectedBackgroundViewForTableView:(UITableView*)tableView单元格:(UITableViewCell*)单元格indexPath:(NSIndexPath*)indexPath{
int64_t delayUnseconds=0;
dispatch\u time\u t popTime=dispatch\u time(立即分派时间,延迟间隔*秒秒秒);
调度\u after(popTime,调度\u get\u main\u queue(),^(void){
[[self class]roundCornersForSelectedBackgroundViewForTableView:tableView单元格:单元格indexPath:indexPath];
});
}
-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(NSIndexPath*)indexPath{
//cell.selectedBackgroundView.frame尚未正确调整大小,因此必须暂时推迟圆角
[[self class]延迟RoundCornersForSelectedBackgroundViewforTableView:tableView单元格:单元格indexPath:indexPath];
}
@结束

谢谢Darktau,我认为这是现在最好的解决方案。