UIEdgeInsetsMake在单元格上创建一个奇怪的乐队,我不知道如何解决它

Dou*_*ith 9 objective-c uitableview uiimageview ios uiedgeinsets

我正在尝试将UIEdgeInsetsMake我的单元格的背景设置为渐变.我已尝试过多种方法让它发挥作用,但无论我使用什么,总会出现问题.

我只是有两个静态细胞,在那里我试图设置他们backgroundViewwillDisplayCell:.我有顶部,底部和中间单元的单独图像,但由于我有两个单元格,我只需要顶部和底部.这些是这些图像:

最佳

在此输入图像描述

底部

在此输入图像描述

底部顶部有一条缺失线,因此它们之间没有2pt线.我稍微调整了底部的高度以补偿(高1pt).这些图像是44x44pt.

我把它们设置如下:

- (void)tableView:(UITableView *)tableView 
  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        UIImageView *topCellBackgroundImageView = 
        [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grouped-cell-bg-top"]
               resizableImageWithCapInsets:UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0)]];
        cell.backgroundView = topCellBackgroundImageView;
    }
    // If it's the last row
    else if (indexPath.row == ([tableView numberOfRowsInSection:0] - 1)) {
        UIImageView *bottomCellBackgroundImageView = 
        [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"grouped-cell-bg-bottom"] 
               resizableImageWithCapInsets:UIEdgeInsetsMake(5.0, 5.0, 5.0, 5.0)]];
        cell.backgroundView = bottomCellBackgroundImageView;
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Run Code Online (Sandbox Code Playgroud)

这不行.正如您在下面的图片中所看到的,在顶部单元格中,整个单元格中有一条1pt白色"带",看起来非常难看.我不知道为什么会这样.

在此输入图像描述

所以我改变了它的topCellBackgroundView边缘插入(5.0, 5.0, 0.0, 5.0),因为它只是在顶部四舍五入,所以bottom不需要考虑属性(它是平的).这很完美!除非您选择单元格,否则底部单元格不再占用其整个单元格.

在此输入图像描述

我应该做些什么?似乎无论我做什么,它都行不通.我也试过1.0而不是0.0,以及-1.0无济于事.

jar*_*air 10

好消息:你不会发疯.您的可伸缩图像代码可能是完美的.问题是,除了返回的值之外,具有分组样式的UITableView 还会为单元格的高度添加额外的点heightForRowAtIndexPath:

因此,您的问题的解决方案是heightForRowAtIndexPath:根据该部分中的总行数返回调整后的单元格高度:

- (CGFloat)heightForRow:(NSIndexPath *)indexPath {
    CGFloat standardHeight = 44.0f;
    NSInteger numberOfRowsInSection = [self numberOfRowsInSection:indexPath.section];

    if (numberOfRowsInSection == 1) {
        standardHeight -= 2;
    }
    else if (indexPath.row == 0 || indexPath.row == numberOfRowsInSection-1) {
        standardHeight -= 1;
    }

    return standardHeight;
}
Run Code Online (Sandbox Code Playgroud)

这是一个示例图像,显示了UITableView如何做到这一点:

uitable  - 视图 - 分组式-borked

据我所知,只有分组的样式表视图受到影响,即使这样,效果也会根据给定部分中的总行数而变化:

  1. 一行向单元格高度添加两个点(默认高度为视网膜上的92像素).
  2. 行将一个点添加到第一行和最后一行的高度(默认情况下视网膜上每个行高90像素).
  3. 三行将一个点到第一行和最后一行的高度(90个像素高的每个视网膜上默认情况下).中间行不受影响.

这是令人沮丧的,据我所知从未记录过.:-)

更新

上面的计算是针对使用默认分隔符样式的分组样式表视图UITableViewCellSeparatorStyleSingleLineEtched.以下是在其他情况下应该做的事情(即UITableViewCellSeparatorStyleSingleLineUITableViewCellSeparatorStyleNone):

- (CGFloat)tableView:(UITableView *)tableView 
   heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat rowHeight = 44.0f;
    if (indexPath.row == 0) {
        rowHeight -=1;
    }
    return rowHeight;
}
Run Code Online (Sandbox Code Playgroud)