UITableViewCell突出显示隐藏分隔线

时间:2013-06-12 13:25:35

标签: iphone ios uitableview

我希望UITableView拥有自定义UITableViewCell和自定义分隔符。

据我所知,没有单独的“分隔符视图”,所以我必须在单元格本身或单元格底部包含分隔符图像。

当我在选择后突出显示一个单元格时,我只能影响单元格自身的外观 - 所以虽然可以隐藏相关单元格中的分隔符图像,但是上方(或下方)单元格中的分隔符图像仍然存在可见

是否有任何(简单可靠的)隐藏 BOTH 分隔线的方法?

更好的解释:

我想做的是:左图显示带有分隔符的细胞(红线)(粗黑线),右图显示选择的中间细胞 - 隐藏中间细胞的绿色选择图像分隔符图像和顶部单元格的分隔符图像。

Cells with separators Selected cell hiding two separators

我已经尝试将选择图像的帧设置为-3 y位置,但这没有用,因为表格单元格视图按从高到低的顺序绘制...

4 个答案:

答案 0 :(得分:2)

好吧,如果你想在选择一个单元格后操纵两个单元格(或更多单元格),那么你必须向tableView询问这些单元格。
例如,如果所选单元格的IndexPath为0.4,则必须在tablePath中询问indexPath 0,3处的单元格。

所以在你的didSelectRowAtIndexPath方法中你可以添加:


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *ip = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section];
    UITableViewCell *previousCell = [self tableView:tableView cellForRowAtIndexPath:ip];
}
然后,您可以隐藏该单元格中的行。

如果使用此方法,请不要忘记检查新indexPath的边界。例如,行/部分低于0或高于方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSIndexPath *ip = [NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]; UITableViewCell *previousCell = [self tableView:tableView cellForRowAtIndexPath:ip]; }[tableView:numberOfSections]返回的indexPath。

答案 1 :(得分:0)

selectedBackgroundView上使用UITableViewCell的{​​{1}}属性:

cellForRowAtIndexPath

答案 2 :(得分:0)

我想建议你一个方法,但不确定它是否正确。尝试为分隔线imageview设置空白或清晰图像highlightedImage

答案 3 :(得分:0)

嗯,过了一会儿回答我自己的问题: - )。

我找到了一个很好的方法:

我真正想做的是将有问题的单元格放到前面,然后在其中绘制一个稍大的选定状态图像,这样它也会隐藏前一个单元格的分隔符。问题是无法保证哪个表格单元格在任何时间位于哪个其他单元格之前。

解决方案非常简单:确保所选单元格位于所有其他单元格的前面!

所以我的自定义 UITableViewCell 现在可以执行以下操作:

-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [self showSelected];
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlightedanimated:animated];
    [self showSelected];
}

-(void)showSelected {
    if(self.selected || self.highlighted)
    {
        self.backgroundImage.hidden = false;  // show the selected image (2 pixels larger than the cell, origin (0, -2) -> paints over previous cell's separator)
        self.cellSeparator.hidden = true;     // hide my cell's separator image

        // make sure the cell is in front of all other cells!
        [self.superview bringSubviewToFront:self];
    }
    else
    {
        self.backgroundImage.hidden = true;    // hide the selected image again
        self.cellSeparator.hidden = false;     // show my cell's separator image
    }
}

呃瞧,一切都很好。