使用UITableView Style Grouped后,使用带自定义颜色的selectedBackgroundView时会出现问题。
它在那里画UITableViewCell。 是否可以将它们绑定到绑定?
答案 0 :(得分:5)
我有类似的问题,没有找到任何简单易行的方法来解决。我为UIImage制作了类别,并使用了几张图片来捕获所有案例。
不那么优雅但正在工作
@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;
答案 1 :(得分:2)
这是一个真正的解决方案,不使用图像,应该适用于所有情况。由于tableView:willDisplayCell:forRowAtIndexPath:仅在iPad上出现问题(见注释),这有点棘手。除此之外,一旦你知道如何围绕个别角落,事情会很自然地落到位。
控制器是圆角的控制器,因为它是知道单元所在位置(第一个,最后一个等)并且影响哪些角被舍入的控制器。进行舍入的最佳位置似乎在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
答案 2 :(得分:1)
UIView * myCellView = [[UIView alloc] initWithFrame:cell.frame];
myCellView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
cell.selectedBackgroundView = myCellView;
[myCellView release];