gpt4 book ai didi

iphone - 在 UITableView 样式中自定义 selectedBackgroundView 出站分组

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:44 27 4
gpt4 key购买 nike

enter image description here使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com