0

I have UITableViewCell in my app:

@interface ResultCell : UITableViewCell {
IBOutlet UILabel *name;
IBOutlet UILabel *views;
IBOutlet UILabel *time;
IBOutlet UILabel *rating;
IBOutlet UILabel *artist;

IBOutlet UIImageView *img;
}

@property (nonatomic, retain) UILabel *name;
@property (nonatomic, retain) UILabel *views;
@property (nonatomic, retain) UILabel *time;
@property (nonatomic, retain) UILabel *rating;
@property (nonatomic, retain) UILabel *artist;

@property (nonatomic, retain) UIImageView *img;

@end

And all of this IBOutlet connected in the Xib file to UILabel....

This is how i create each cell:

static NSString *CellIdentifier = @"ResultCell";
ResultCell *cell = (ResultCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){
    UIViewController *vc = [[[UIViewController alloc] initWithNibName:@"ResultCell" bundle:nil] autorelease];
        cell = (ResultCell *) vc.view;
}

cell.name.text = item.name;
cell.views.text = item.viewCount;
cell.rating.text = [NSString stringWithFormat:@"%d%%",item.rating];
cell.time.text = item.timeStr;
cell.artist.text = item.artist;

And i want to know if in the ResultCell class i need to implement a dealoc method and release the UILabel ? or it's ok like what i done? i am using Non-ARC because it is an old project.

Tim
  • 8,932
  • 4
  • 43
  • 64
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • why do you create e UIViewController, and then take the cell, instead of a cell? your vc.view seems to be a UIView, not a cell. – Marco Pace May 08 '13 at 09:03
  • In this case, yes you would need to. However, it seems pointless to have all of those retain properties since they will be retained by their superview anyway. Why not change them all to `assign`? Then you won't have to deal with `dealloc`. – borrrden May 08 '13 at 09:04
  • why do u need a UIViewController for ResultCell class? – Ishank May 08 '13 at 09:05
  • because using a UIViewController for loading a cell from a NIB is probably the best way to do it – drct May 08 '13 at 10:36

5 Answers5

2

Yes, every retained property or instance variable has to be released and IBOutlets are no different. Because you use properties, the preferable way to do this is:

-(void)dealloc {
    self.name = nil;
    self.views = nil;
    //... and so on
    [super dealloc];
}

By the way, you don't need to declare "redundant" instance variables for your properties like this:

IBOutlet UILabel *name;

It was needed a long time ago (AFAIR in times of XCode 3), but now the compiler will generate them automatically for each declared property.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
  • 1
    +1, but just a minor complaint that I read: Don't use `self.XXX = nil` inside `dealloc` and instead just release the backing property. This will avoid any potential KVO issues (It is best to avoid `self` inside of `init` and `dealloc` wherever possible) – borrrden May 08 '13 at 09:28
  • Link to the above info -> http://stackoverflow.com/questions/3402234/properties-in-dealloc-release-then-set-to-nil-or-simply-release#comment3539445_3402247 – borrrden May 08 '13 at 09:30
  • IBOutlets are held by the view and can be weak -- except for top level objects – Daij-Djan May 08 '13 at 09:38
  • @Daij-Djan Yes, that is also true (as I noted in a comment on the original post). However, in the case that OP doesn't make them assign properties this is a sound response. – borrrden May 08 '13 at 10:15
0

You can make all labels in the custom table viewcell as assign if you need it retain it you have to release in dealloc method and assign nil in viewDidUnload.It avoids memory leaks.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
jailani
  • 2,260
  • 2
  • 21
  • 45
0

Yes, You have to write the dealloc method in ResultCell class to release the objects that you synthesize to avoid memoryleak. For more understanding refer the link http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-2

LittleIDev
  • 921
  • 6
  • 7
0
  1. You are type casting UIView as ResultCell, instead of creating UITableViewCell instance with cell identifier.
  2. dequeueReusableCellWithIdentifier: will always return nil, thus creating large number of UIViewController instances
  3. Since you are returning UIViewController's view, memory management will become tricky. You also need to release view controller instance after the cell is released by the table view. This will require to store all vc instances and release them later on. Currently all vc instances are causing memory leaks.

To avoid these unnecessary complications you need to follow standard techniques. That being said, you need to create a separate XIB and class files for cell, use UINib to load the table view cell from xib. Refer this tutorial.

Cheers!
Amar

Amar
  • 13,202
  • 7
  • 53
  • 71
-1

use something like-

ResultCell *containerView = [[[NSBundle mainBundle] loadNibNamed:@"ResultCell"  owner:self    options:nil] lastObject];

instead of

UIViewController *vc = [[[UIViewController alloc] initWithNibName:@"ResultCell" bundle:nil] autorelease];

Make sure u make the ResultCell.xib files' owner to ResultCell class.

And Yes, as the UILabel and UIImageView are retained properties, they shall be released in the dealloc method of ResultCell class.

Unnati
  • 2,441
  • 17
  • 37
Ishank
  • 2,860
  • 32
  • 43