Mam pewne problemy z niestandardowym UITableViewCell i jak zarządzać rzeczy za pomocą storyboardów. Kiedy umieszczam kod stylizacji w initWithCoder:
, to nie działa, ale jeśli umieściłem go w tableView: cellForRowAtIndexPath:
, to działa. W scenorysie mam prototypową komórkę z atrybutem klasy ustawionym na moją niestandardową klasę UITableViewCell. Teraz kod w initWithCoder:
zostaje wywołany.Stylizacja niestandardowy UITableViewCell w initWithCoder: nie działa
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];
//sets the text of the labels
id<SimoListItem> item = (id<SimoListItem>) [self.places objectAtIndex:[indexPath row]];
cell.mainLabel.text = [item mainString];
cell.subLabel.text = [item subString];
//move the labels so that they are centered horizontally
float mainXPos = (CGRectGetWidth(cell.contentView.frame)/2 - CGRectGetWidth(cell.mainLabel.frame)/2);
float subXPos = (CGRectGetWidth(cell.contentView.frame)/2 - CGRectGetWidth(cell.subLabel.frame)/2);
CGRect mainFrame = cell.mainLabel.frame;
mainFrame.origin.x = mainXPos;
cell.mainLabel.frame = mainFrame;
CGRect subFrame = cell.subLabel.frame;
subFrame.origin.x = subXPos;
cell.subLabel.frame = subFrame;
return cell;
}
I debugowania kodu i stwierdził, że dequeue...
nazywa się pierwszy, a następnie przechodzi do initWithCoder:
a następnie powrót do kodu kontrolera widoku. Dziwne, że adres komórki w pamięci zmienia się między return self;
i kiedy wraca do kontrolera. A jeśli przeniesię kod stylizacji z powrotem do kontrolera widoku po dequeue...
wszystko działa poprawnie. Po prostu nie chcę robić niepotrzebnych stylizacji przy ponownym użyciu komórek.
Cheers