0

我试图找出一种方法,使我的单元格 selectedBackgroundView 大于单元格本身。

我的以下方法旨在制作 selectedBackgroundView:单元格的超级视图宽度(屏幕的整个宽度)X单元格的高度

UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.superview.frame), CGRectGetHeight(cell.frame))];
bg.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = bg;

选择此单元格时,背景颜色仅在单元格的范围内扩展(不达到超级视图的高度)

我怎样才能最好地完成使 selectedBackgroundView 大于单元格?我在这里采取了完全错误的方法吗?

4

1 回答 1

2

正如我测试的那样,selectedBackgroundView的框架是恒定的,你bg的宽度变化不起作用。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];
    CGRect f = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
    f.size.height += 20;
    f.origin.y -= 10;
    UIView *bg = [[UIView alloc] initWithFrame: f];
    bg.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview: bg];
    cell.contentView.clipsToBounds = false;
}
于 2020-06-05T03:51:37.697 回答