0

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?

Houman
  • 64,245
  • 87
  • 278
  • 460

2 Answers2

1

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];
Gralex
  • 4,285
  • 7
  • 26
  • 47
  • ahh now that I instantiate my chart class via `initWithFrame`, its drawRect is actually hit. And I see a black rectangle in the cell. So I need to define that rectangle's position to make it appear exactly where I want it to be. Thanks for the tip. Now one major problem I come across is that only the cells inside the view have the custom drawing. The cells outside the view that are dragged into screen remain empty. Even though I have already added `[cell setNeedsDisplay]; [chart setNeedsDisplay]; [cell.contentView setNeedsDisplay];` – Houman Nov 10 '13 at 17:55
  • Ignore the second part of my comment. It was due a mistake in my code. It works perfectly. So `initWithFrame` was the solution after all. thanks +1 – Houman Nov 10 '13 at 20:37
0

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];
user1416564
  • 401
  • 5
  • 18
  • Thanks. I have `[cell setNeedsDisplay]; [chart setNeedsDisplay];` already without joy. But I can't do `[chart.view setNeedsDisplay];` It says view not found on chart. – Houman Nov 10 '13 at 16:46