PDA

View Full Version : Multiline UITableViewCell subclass causes problems...




Aquis
Apr 1, 2009, 11:41 AM
Hello all, I created a UITableViewCell subclass called UITableViewMultiLineCell—half the time it works, but at other times it just causes graphical bugs with other and its own table view, and sometimes causes crashes. Here is how I created it:

#import "UITableViewMultiLineCell.h"


@implementation UITableViewMultiLineCell

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
UIFont * font = [UIFont systemFontOfSize:17];

label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, frame.size.width - 20, frame.size.height - 20)] autorelease];
label.font = font;
label.numberOfLines = 0;

[[self contentView] addSubview:label];
}
return self;
}

- (CGFloat)heightForCellOfWidth:(CGFloat)width {
label.frame = CGRectMake(10, 10, width - 20, self.bounds.size.height - 20);

[label sizeToFit];

label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, width - 20, label.frame.size.height);

return label.frame.size.height + 20.0;
}

- (void)setText:(NSString *)text {
label.text = text;
}

- (NSString *)text {
return label.text;
}

- (void)dealloc {
[super dealloc];
}


@end

This is what it's meant to look like: Link (http://towersnerd.com/images/robert/multibug/Correct.png)

This is what it sometimes looks like: 1 (http://towersnerd.com/images/robert/multibug/Wrong1.png) 2 (http://towersnerd.com/images/robert/multibug/Wrong2.png) 3 (http://towersnerd.com/images/robert/multibug/Wrong3.png)

When it crashes, I don't believe my code is (directly) responsible, according to the debugger (http://towersnerd.com/images/robert/multibug/DebugInfo.png).

Does anyone have any ideas on why this is happening, or have created their own successful implementation?

Thanks!



TPALTony
Apr 3, 2009, 07:35 PM
This is just a guess as I'm fairly new to this too.... but your init method doesn't appear to call the super class init method. I assume you're sub-classing UITableViewCell and I suspect a lot of the logic to handle curves edges, margins etc is all in the parent class. If you're not calling the super init method then the odds are you're missing a lot of that logic.

Just a guess! :)

t

EDIT: This is what happens when you code after a migraine. OK, so you ARE calling the parent class init method... In that case I'm a tad confused too. Sorry!