I have some problems with a custom UITableViewCell and how to manage things with a storyboard. When I put the style code in initWithCoder: it does not work, but if I put it in tableView: cellForRowAtIndexPath: it works. In the storyboard, I have a prototype with my class attribute set to a custom class UITableViewCell. Now the code in initWithCoder: is called.
SimoTableViewCell.m
@implementation SimoTableViewCell @synthesize mainLabel, subLabel; -(id) initWithCoder:(NSCoder *)aDecoder { if ( !(self = [super initWithCoder:aDecoder]) ) return nil; [self styleCellBackground]; //style the labels [self.mainLabel styleMainLabel]; [self.subLabel styleSubLabel]; return self; } @end
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"NearbyLandmarksCell"; SimoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
I debugged the code and found that dequeue... is called first, then it goes into initWithCoder: and then back to the view controller code. It is strange that the address of a cell in memory changes between return self; and when he returns to the controller. And if I return the style code back to the view controller after dequeue... everything will be fine. I just donβt want to make unnecessary style when reusing cells.
Greetings
chopchop
source share