使用 UITableView Style Grouped 后,使用带有自定义颜色的 selectedBackgroundView 时会出现问题。
它在 UITableViewCell 之外绘制。 是否可以将它们夹在边界内?
最佳答案
我也遇到过类似的问题,没有找到简单易行的方法解决。我为 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
在 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;
关于iphone - 在 UITableView 样式中自定义 selectedBackgroundView 出站分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8799768/