0

In my project need to add uiview in UITableViewCell. I can add UIView using following code in UITableViewCell class.

    -(void)drawRect{
       [cell.contentView addSubview:myView];
    } 

Is this a efficient way to show when large amount of table cell are displaying. Is there other way to add UIView using method like.

    drawInRect
Susitha
  • 3,339
  • 5
  • 27
  • 41
  • this question is vague at best so don't expect a straight answer. If you just want to `-addSubview:` then I **don't** believe this is the right place for it. – staticVoidMan Aug 28 '14 at 05:56
  • `cellForRowAtIndexPath:` is the best place to do so, not `drawRect`. However, if this subview of yours will be added in a lot of cells, why not make it a property of your subclassed cell, and toggle `hidden`/`frame` when you need to show/hide it? – n00bProgrammer Aug 28 '14 at 06:31

2 Answers2

0

You will want to add the subView in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath which is part of UITableViewDataSource protocol.

twodayslate
  • 2,803
  • 3
  • 27
  • 43
0

Don't use drawRect method that way. That method is using for drawing some parts of the ui-element and may call several times per a sec. You should add a subview in cellForRowAtIndexPath method (UITableViewDataSource protocol) or in - (void)awakeFromNib. Don't forget to remove that subview in - (void)prepareForReuse method of your UITableViewCell subclass.

Daniyar
  • 2,975
  • 2
  • 26
  • 39