未显示将子视图添加到UITableViewCell的情况 [英] Adding a SubView to UITableViewCell doesn't get displayed

查看:55
本文介绍了未显示将子视图添加到UITableViewCell的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 drawRect

@interface FTChartsView : UIView

@implementation FTChartsView

-(void)drawRect:(CGRect)rect
{
   ...
}

我也将UITableViewCell子类化

I have also subclassed the UITableViewCell

@interface FTSummaryCellView : UITableViewCell
...

现在,在ViewController中,当生成单元格时:

Now in the ViewController, when the cells are generated:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UINib *nib = [UINib nibWithNibName:@"FTSummaryCellView" bundle:nil];
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"FTSummaryCellView"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   FTSummaryCellView *cell = [tableView dequeueReusableCellWithIdentifier:@"FTSummaryCellView"];
   FTChartsView *chart = [[FTChartsView alloc] init];
   [cell addSubview:chart];
   return cell;
}

单元格将chatrView添加为子视图.但是,ChartsView的 drawRect 永不中断,因此也不会显示.

The cell gets the chatrView added as subview. However chartsView's drawRect, is never breaking and hence never showing.

请问我在这里想念什么?

What am I missing here please?

推荐答案

您忘记了 setFrame .

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    TView *chart = [[TView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
    [cell addSubview:chart];
    return cell;
}

对我来说很完美.

Ty代表 [[self tableView] registerNib:nib forCellReuseIdentifier:@"FTSummaryCellView"]; .我从不知道这种方法)

Ty for [[self tableView] registerNib:nib forCellReuseIdentifier:@"FTSummaryCellView"];. I never known about this method)

您必须将子视图添加到内容视图,如我所知.但是,所有这些对我来说都是有效的.

You must add subview to content view as I known. But all works for me without that.

[cell.contentView addSubview:chart];

这篇关于未显示将子视图添加到UITableViewCell的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆