PDA

View Full Version : UILabel Not Displaying Properly




matthew858
Mar 13, 2009, 10:41 PM
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:




- (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.



PhoneyDeveloper
Mar 14, 2009, 03:08 PM
The problem with the UILabel is that its not adding the 2nd label to the table view cell.

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 #).

matthew858
Mar 14, 2009, 06:14 PM
Do you really want a font size of 1?

I did this as a part of a test to see whether the code was displaying or not, and forgot to remove it.

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.
Fixed my code (in Xcode).

Use the code style not quote style for displaying code in this forum (code is the one marked #).
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.

PhoneyDeveloper
Mar 15, 2009, 03:48 PM
The code is executing fine, but with a warning (passing argument 1 of 'setText' from distinct objective-c type).

Which line is this warning on? It's most likely the cause of the problem.

matthew858
Mar 15, 2009, 03:52 PM
Which line is this warning on? It's most likely the cause of the problem.

The line almost right at the bottom:

infoLabel.text = store.info;

dejo
Mar 15, 2009, 03:58 PM
The line almost right at the bottom:

infoLabel.text = store.info;

UILabel setText (which is what infoLabel.text is really calling) expects an NSString *. store.info seems to be something else.