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

标签 iphone ios uitableview

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/

相关文章:

iphone - 突出显示 PDF 文档 iPhone xcode 中的文本

iphone - Apple 会拒绝使用 sksmtpmessage 框架在后台发送电子邮件的应用程序吗?

ios - 颜色选择器的困境

ios - UIStackView 没有显示阴影

html - 将 HTML 表格抓取到 UITableView

ios - xcode - 从另一个类调用方法后刷新 TableView

iphone - 如何重用 UIControl 从 iPhone xib 到 iPad xib

iphone - 应用崩溃:内存不足

php - 从 iOS 获取我的 JSON 以发布到我的 php 文件并插入到 mySQL 时遇到问题

iOS:编辑 TableView 中的项目问题