更新:
所以我做了更多的实验,以下解决方案仍然有效,而无需将单元格的背景设置为透明,这涉及移动被覆盖单元格的 z 顺序。这适用于突出显示和选择另一个单元格(通过相关回调),如果两个单元格的背景是不同的颜色。解决方法如下(didHighlight 和didSelect 方法对你来说不重要,你可以忽略它们):
(请注意,“覆盖行”是我们试图使其内容保持可见的行,在我的情况下,它的内容会稍微进入上面的行,它正在剪裁它)
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == ROW_ABOVE_COVERED_ROW)
{
NSIndexPath * rowbelow = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:rowbelow];
[cell.superview bringSubviewToFront:cell];
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == ROW_ABOVE_COVERED_ROW)
{
NSIndexPath * rowbelow = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:rowbelow];
[cell.superview bringSubviewToFront:cell];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0 && indexPath.row == COVERED_ROW)
{
[cell.superview bringSubviewToFront:cell];
cell.contentView.superview.clipsToBounds = NO;
}
}
注意:您还应该将内容的背景颜色设置为清除,否则它将采用单元格其余部分的 bgcolor,因此当您设法将内容带到覆盖单元格的前面时,它将带上背景颜色并在另一个单元格中留下一个令人讨厌的块(在我的情况下,我唯一的内容是detailTextLabel 和textLabel):
// in cellForRowAtIndexPath:
[cell setBackgroundColor:[UIColor redColor]]; //using red for debug
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
我希望这对尝试这个的其他人有所帮助....
原文:
对我来说,解决方案是使用:
self.contentView.superview.clipsToBounds = NO;
我的单元格已经是透明的,但我的内容仍然被剪切。在我的例子中,我使用了一个自定义单元格,它将其内容向上移动到layoutSubviews。所以layoutSubviews 我的自定义单元格如下:
-(void)layoutSubviews
{
[super layoutSubviews];
self.contentView.frame = CGRectOffset(self.contentView.frame, 0, -11);
self.contentView.superview.clipsToBounds = NO;
}
我不知道如果上面的单元格是不透明的,这是否会起作用,或者如果单元格在按下时会突出显示,这是否会掩盖我的内容。
但是,我没有需要在 viewWillDisplayCell 回调方法中再次使单元格透明 - 在正常的 cellForRowAtIndexPath 中这样做就足够了 p>