我需要在纯 UItableView 中使单元格 selectionBackgroundView 不全宽。为此,我创建了 UItableViewCell 子类并覆盖了 layoutsubviews 方法
- (void)layoutSubviews
{
[super layoutSubviews];
self.selectedBackgroundView.frame = CGRectMake(self.frame.origin.x + 10.0f, self.frame.origin.y, self.frame.size.width - 20.0f, self.frame.size.height);
}
我的 cellForRowAtIndexPath 方法看起来像
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat: @"CELL %i %i", indexPath.section, indexPath.row];
GroupedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil)
{
cell = [[GroupedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *cellBgView = [[UIView alloc] init];
cellBgView.frame = CGRectMake(10, 0, 300, 80);
[cellBgView setBackgroundColor:[UIColor colorWithRed:242 / 255.0 green:242 / 255.0 blue:242 / 255.0 alpha:1.0]];
[cell.contentView addSubview:cellBgView];
}
[cell setBackgroundColor:[UIColor clearColor]];
UIView *selectionView = [[UIView alloc] init];
[selectionView setBackgroundColor:[UIColor colorWithRed:181 / 255.0 green:211 / 255.0 blue:53 / 255.0 alpha:1.0]];
selectionView.frame = CGRectMake(10, 0, 300, 80);
cell.selectedBackgroundView = selectionView;
return cell;
}
但只有第一行 selectedView 工作正常。对于其他行,我选择了颜色清晰的 View 。请帮帮我。
最佳答案
我只需要用数字设置框架。
- (void)layoutSubviews
{
[super layoutSubviews];
self.selectedBackgroundView.frame = CGRectMake(10.0f, 0, 300, 80);
}
关于ios - 子类 uitableviewcell 并覆盖 layoutsubviews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20145454/