Multiline UITableViewCell subclass causes problems...

Discussion in 'iOS Programming' started by Aquis, Apr 1, 2009.

  1. Aquis macrumors member

    Aug 7, 2007
    Staffordshire, UK
    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];
    This is what it's meant to look like: Link

    This is what it sometimes looks like: 1 2 3

    When it crashes, I don't believe my code is (directly) responsible, according to the debugger.

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

  2. TPALTony macrumors regular

    May 29, 2007
    A guess

    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! :)


    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!

Share This Page