[我正在尝试找出一种使单元格的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大于单元格?我在这里采用完全错误的方法吗?
如我所测试的,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;
}
@ dengApro的答案做到了。
f.size.width += 10;
UIView *bg = [[UIView alloc] initWithFrame: f];
bg.backgroundColor = [UIColor cellSelected];
[cell.superview addSubview: bg];
cell.superview.clipsToBounds = false;```