vbw*_*bwx 2 iphone cocoa-touch uikit ios
我有这个表,其单元格如下所示:

如您所见,每个单元格的顶部边框应该与上面的单元格重叠.现在,这个边框是背景的一部分(到目前为止没有重叠).
现在,我将边框与背景分开,我的单元格创建代码如下所示:
#define QAImage(NAME) [[[UIImageView alloc] initWithImage:[UIImage imageNamed:NAME]] autorelease]
- (UITableViewCell*) tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath*)index
{
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Entry"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Entry"] autorelease];
cell.backgroundView = QAImage(@"bg-cell.png");
cell.selectedBackgroundView = QAImage(@"bg-cell-sel.png");
cell.selectionStyle = UITableViewCellSelectionStyleGray;
UIImageView *img = QAImage(@"disclosure.png");
img.highlightedImage = [UIImage imageNamed:@"disclosure-sel.png"];
cell.accessoryView = img;
CGRect r = cell.frame;
r.size.height = table.rowHeight;
r.size.width = table.frame.size.width;
cell.frame = r;
r = cell.contentView.frame;
r.origin = CGPointMake(13, 13);
r.size.width -= 8 + img.frame.size.width;
r.size.height -= 25;
[cell.contentView addSubview:[[[TagView alloc] initWithFrame:r] autorelease]];
img = QAImage(@"cell-top.png");
img.highlightedImage = [UIImage imageNamed:@"cell-top-sel.png"];
img.opaque = NO;
r = img.frame;
r.origin = CGPointMake(0, 1 - r.size.height); // The (supposed) overlapping happens here.
img.frame = r;
cell.contentView.clipsToBounds = NO;
[cell.contentView addSubview:img];
}
// Here I set the title. (omitted)
return cell;
}
Run Code Online (Sandbox Code Playgroud)
问题是,边框仅在我在表格中向上滚动时显示,否则它本身与其上方的单元格重叠.(错误的方式.)
我已经尝试[cell setNeedsDisplay]过-tableView:willDisplayCell:forRowAtIndexPath:,cell-top.png每次请求一个单元格时添加border()(这显然会导致多次绘制并且看起来不太好;无论如何,它仍然无法解决我的问题)和设置img.layer.zPosition = 2.
如何强制UIImageView始终保持最佳状态?
我的建议是继承UITableView并覆盖layoutSubviews,将可见的单元格视图放入你想要它们的z顺序中,如下所示:
- (void)layoutSubviews {
[super layoutSubviews];
NSArray *sortedIndexPaths = [[self indexPathsForVisibleRows] sortedArrayUsingSelector:@selector(compare:)];
for (NSIndexPath *path in sortedIndexPaths) {
UITableViewCell *cell = [self cellForRowAtIndexPath:path];
[self bringSubviewToFront:cell];
}
}
Run Code Online (Sandbox Code Playgroud)
这可能是太慢了,或者你可能需要更改bringSubviewToFront:到sendSubviewToBack:,但希望这给你一个方向尝试.
编辑:现在的另一个选择,如果你的目标是iOS 6,那就是使用UICollectionView,它内置了对z-ordering的支持.
| 归档时间: |
|
| 查看次数: |
5256 次 |
| 最近记录: |