ios - 子类 uitableviewcell 并覆盖 layoutsubviews

标签 ios objective-c uitableview

我需要在纯 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/

相关文章:

objective-c - 任何理解 iOS 框架的路线图?

objective-c - macOS开发中如何关闭新窗口?

objective-c - Mac OS X cocoa 应用程序中的互联网连接通知

ios - 新手 ios - MVC 中的模型在哪里?

iphone - 如何制作可扩展的 UITableViewCell

objective-c - 为什么 iOS 在通过 UIManagedDocument 保存核心数据时会延迟?

ios - iOS 8 中的 CLLocationManager 故障排除

ios - 通过 TestFlight 安装 iOS 应用是否模拟应用升级?

ios - 动态添加对象到 UITableView 单元格

ios - DidSelectRowAt ..什么也没发生