Dou*_*ith 9 objective-c uitableview uiimageview ios uiedgeinsets
我正在尝试将UIEdgeInsetsMake
我的单元格的背景设置为渐变.我已尝试过多种方法让它发挥作用,但无论我使用什么,总会出现问题.
我只是有两个静态细胞,在那里我试图设置他们backgroundView
在willDisplayCell:
.我有顶部,底部和中间单元的单独图像,但由于我有两个单元格,我只需要顶部和底部.这些是这些图像:
最佳
底部
底部顶部有一条缺失线,因此它们之间没有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如何做到这一点:
据我所知,只有分组的样式表视图受到影响,即使这样,效果也会根据给定部分中的总行数而变化:
这是令人沮丧的,据我所知从未记录过.:-)
更新
上面的计算是针对使用默认分隔符样式的分组样式表视图UITableViewCellSeparatorStyleSingleLineEtched
.以下是在其他情况下应该做的事情(即UITableViewCellSeparatorStyleSingleLine
和UITableViewCellSeparatorStyleNone
):
- (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)
归档时间: |
|
查看次数: |
2521 次 |
最近记录: |