UILabel Not Displaying Properly

Discussion in 'iOS Programming' started by matthew858, Mar 13, 2009.

  1. matthew858 macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #1
    I am trying to add a 2nd UILabel to a UITabView below the main label to add more description to what you will find when you tap on the table view (like the mail preview below the main title of the email to provide more info on its contents).

    The problem with the UILabel is that its not adding the 2nd label to the table view cell.

    The code is:

    Code:
    
    
    - (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    	
        if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
    		store = nil;
    		storeTileView = nil;
    		labelView = nil;
    		infoLabel = nil;
    		
    		// create the storeTileView and the labelView
    		// both of these will be laid out again by the layoutSubviews method
    		AtomicstoreTileView *tileView = [[AtomicstoreTileView alloc] initWithFrame:CGRectZero];
    		self.storeTileView = tileView;
    		[self.contentView addSubview:tileView];
    		[tileView release];
    		
    		
    		UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    		// set the label view to have a clear background and a 20 point font
    		label.backgroundColor = [UIColor clearColor];
    		label.font = [UIFont boldSystemFontOfSize:12.5];
    		self.labelView = label;
    		[self.contentView addSubview:label];
    		[label release];
    		
    		UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    		// set the label view to have a clear background and a 20 point font
    		infoLabel .backgroundColor = [UIColor clearColor];
    		infoLabel .font = [UIFont boldSystemFontOfSize:1];
    		self.infoLabel = label;
    		[self.contentView addSubview:infoLabel ];
    		[infoLabel  release];
    		
    		
    		// add both the label and storeTile to the TableViewCell view
        }
        return self;
    }
    
    
    - (void)layoutSubviews {
    	[super layoutSubviews];
    	
    	// determine the content rect for the cell. This will change depending on the
    	// style of table (grouped vs plain)
    	CGRect contentRect = self.contentView.bounds;
    	
    	// position the image tile in the content rect.
    	CGRect storeTileRect = self.contentView.bounds;
    	storeTileRect.size = [AtomicstoreTileView preferredViewSize];
    	storeTileRect = CGRectOffset(storeTileRect,10,3);
    	storeTileView.frame = storeTileRect;
    	
    	// position the elment name in the content rect
    	CGRect labelRect = contentRect;
    	labelRect.origin.x = labelRect.origin.x+56;
    	labelRect.origin.y = labelRect.origin.y+3;
    	labelView.frame = labelRect;	
    	
    	CGRect labelRect2 = contentRect;
    	labelRect2.origin.x = labelRect2.origin.x+65;
    	labelRect2.origin.y = labelRect2.origin.y+3;
    	infoLabel.frame = labelRect2;	
    	
    	
    }
    
    
    - (void)dealloc {
    	[store release];
    	[storeTileView release];
    	[labelView release];
    	[infoLabel release];
        [super dealloc];
    }
    
    
    - (void)setstore:(Atomicstore *)anstore {
    	if (anstore != store) {
    		[store release];
    		[anstore retain];
    		store = anstore;
    	}
    	storeTileView.store = store;
    	labelView.text = store.name;
    	infoLabel.text = store.info;
    	[storeTileView setNeedsDisplay];
    	[labelView setNeedsDisplay];
    	[infoLabel setNeedsDisplay];
    }
    
    
    The item in the code called "label" is my main label with a list of stores. The "info label" is what I want to add to the table view. The infoLabel reads data from store.info (that of which reads data from a .plist file).

    Any ideas would be appreciated.
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Why do you think that?

    A few comments about this code.

    Do you really want a font size of 1?

    You never need to call setNeedsDisplay on labels after setting the text. In fact you should never need to call setNeedsDisplay on any UIKit classes after setting a property on those classes. They know when they need to redraw.

    Use the code style not quote style for displaying code in this forum (code is the one marked #).
     
  3. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #3
    I did this as a part of a test to see whether the code was displaying or not, and forgot to remove it.

    Fixed my code (in Xcode).

    Fixed my original post.


    More Info:
    The code is executing fine, but with a warning (passing argument 1 of 'setText' from distinct objective-c type). The end result is that the contents of labelView (store.name) get overwritten by infoLabel (to store.info), but labelView still keeps its position and font size.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Which line is this warning on? It's most likely the cause of the problem.
     
  5. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #5
    The line almost right at the bottom:
    Code:
    infoLabel.text = store.info;
    
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    UILabel setText (which is what infoLabel.text is really calling) expects an NSString *. store.info seems to be something else.
     

Share This Page