如何在uitableviewcell中添加uiview

如何在uitableviewcell中添加uiview

本文介绍了如何在uitableviewcell中添加uiview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你们一切都好.我的应用程序有一个小问题.我有一个带有自定义单元格的表格视图.在我的自定义单元类中,我做了一个 uiview

Hope all of you'll be fine.I have a little issue in my application. I have a tableview with custom-cell. In my custom-cell class i made a uiview

    UIView* cellView = [[UIView alloc] initWithFrame:self.contentView.frame];
    cellView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:cellView];

问题是 uiview 不适合我的单元格框架.Cellforrow 如下:

Problem is uiview is not fitting in my cell frame properly. Cellforrow is as :

- (SummaryViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    SummaryViewCell *cell = (SummaryViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[SummaryViewCell alloc] init];
    }

    cell.backgroundColor = [UIColor blueColor];
  return cell;
}

我尝试过 initWithFrame:self.contentView.frame] 但发生了同样的问题(uiview 出现在单元格中但只覆盖单元格的一半),我也尝试过 cellView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width 和 height 相同)但失败了.需要您的宝贵建议和想法.非常感谢.

I have tried initWithFrame:self.contentView.frame] but same problem happened (uiview appear in cell but cover only half of the cell), I also have tried cellView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width and same as for height) but failed. Need your valuable advise and ideas. Many Thanks.

推荐答案

您在哪里创建子视图?尝试在 -layoutSubviews 方法中进行.

Where did you creating subview?Try to do it in -layoutSubviews method.

- (void)layoutSubviews {
    [super layoutSubviews];

UIView* cellView = [[UIView alloc] initWithFrame:self.contentView.frame];
    cellView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:cellView];
}

子视图的所有框架都已在此方法中计算出来,您应该收到正确的子视图宽度\高度.

All frames for subviews are already calculated inside this method and you should receive correct width\height for your subview.

这篇关于如何在uitableviewcell中添加uiview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:40