简体   繁体   中英

Adding a SubView to UITableViewCell doesn't get displayed

I have created my own chart via drawRect

@interface FTChartsView : UIView

@implementation FTChartsView

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

I have also subclassed the UITableViewCell

@interface FTSummaryCellView : UITableViewCell
...

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;
}

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?

You forgot about 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;
}

Works perfectly for me.

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

Edit

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

[cell.contentView addSubview:chart];

Here is a stab in the dark... after adding the FTChartView to the cell... maybe try doing:

 [chart setNeedsDisplay];

Sometimes that works if you have a custom drawRect method.

Maybe even

[chart.view setNeedsDisplay];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM