Lable not appearing in UITable Cell - iphone

Have a problem where my Label is not appearing. But the slider does. Its one of these bizarre cases that seems there is no logical reason. Anybody able to make sense of this?
else if (indexPath.row == 4)
{
Filters *filters = [Filters sharedInstance];
CGRect frame = CGRectMake(100, 10, 80, 23);
costSlider = [[[UISlider alloc] initWithFrame:frame] autorelease];
costSlider.maximumValue = [[NSNumber numberWithDouble:
filters.maxCarRentalCost] floatValue];
costSlider.minimumValue = [[NSNumber numberWithDouble:
filters.minCarRentalCost] floatValue];
[costSlider addTarget:self action:#selector(costSliderChanged:)
forControlEvents:UIControlEventValueChanged];
[costSlider setValue:[filters.maxCarCostPerDay floatValue] animated:YES ];
costSlider.continuous = NO;
costSlider.enabled = YES;
costSlider.value = [filters.maxCarCostPerDay floatValue];
costSlider.tag = 5;
[cell addSubview:costSlider];
self.costLabel.textColor = [UIColor greenColor];
self.costLabel.text = #"what"; //[NSString stringWithFormat:#"Cost per day:
%d", filters.maxCarCostPerDay ];
[self.costLabel setBackgroundColor:[UIColor blueColor]];
self.costLabel.frame = CGRectMake(10, 10, 75, 20);
[self.costLabel setFont:[UIFont boldSystemFontOfSize:10.0]];
[cell addSubview:self.costLabel];
}
Thanks
-Code

From the code you posted it does not look like you have alloc'd the label. Unless you are doing that elsewhere in the code, that is probably your issue

Related

MapView annotations change their layer order when touched

Small question, i've got a few annotations on a map view. Sometimes they tend to overlap when they are very close to each other and that's ok, but when i touch them they switch their layer position. Those that were in the back are now in the front and vice versa.
So i checked my didSelectAnnotation function but there is literarily no code in that function so it couldn't be it.
I couldn't think of anything else that might cause it.
Do you know how to fix it so it won't change layer position??
This is my viewForAnnotation code:
annImg = [[szAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
szPointAnnotation *pointAnnotation = annotation;
if (appDelegate.homePage || ([pointAnnotation.tag intValue] >= 10000))
{
annImg.canShowCallout = NO;
annImg.backgroundColor = [UIColor clearColor];
annImg.frame = CGRectMake(0, /*0*/-78, 70, /*78*/156);
UIImageView *currentUserPictureView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 60, 60)];
int newTag = [pointAnnotation.tag intValue]-10000;
UIImage *currentUserPicture;
if ([pointAnnotation.tag intValue] >= 10000) {
currentUserPicture = [UIImage imageWithData:[usersData getKeyForIndexWithKey:#"UserImageData" forIndex:newTag]];
}else{
currentUserPicture = [UIImage imageWithData:[usersData getKeyForIndexWithKey:#"UserImageData" forIndex:[pointAnnotation.tag intValue]]];
}
currentUserPictureView.image = currentUserPicture;
userLocationButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 78)];
[userLocationButton setBackgroundImage:[UIImage imageNamed:#"pin.png"] forState:UIControlStateNormal];
[userLocationButton addTarget:self action:#selector(centerOnUser:) forControlEvents:UIControlEventTouchUpInside];
if ([pointAnnotation.tag intValue] >= 10000) {
userLocationButton.tag = newTag;
}else{
userLocationButton.tag = [pointAnnotation.tag intValue];
}
[userLocationButton setBackgroundColor:[UIColor clearColor]];
[annImg addSubview:userLocationButton];
[userLocationButton addSubview:currentUserPictureView];
[userLocationButton release];
[currentUserPictureView release];
[currentUserPicture release];
}

iphone: how to code different label for each image in scrollview

I successfully created a UIscrollview (horizontal) for 50 images and also created a label of "1 of 50" page number displaying at the top of the image along with the scroll view(for example user scroll to next image it would show 2 of 50, 3 of 50, so forth). I am able to display a label at the bottom of the image as a description. My problem is how can I create or code to display different description for each image? Im thinking of NSarray but it shows some random numbers. Is there a sample I can look at to see what I am doing wrong. you can see the description part where I am lost from there. Thanks =)
CGFloat xOffset = 0;
NSInteger pageNumber = 1;
NSUInteger i;
for (i = 1; i <= 5; i++)
{
NSString *imageName = [NSString stringWithFormat:#"creative%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(xOffset, 0, 320, 288.5)];
[ourScrollView addSubview:imageView];
[imageView release];
UILabel *pageNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, -10, 320, 40)];
[pageNumberLabel setText:[NSString stringWithFormat:#"%d of 5", pageNumber]];
[pageNumberLabel setTextAlignment:UITextAlignmentLeft];
[pageNumberLabel setTextColor:[UIColor whiteColor]];
[pageNumberLabel setBackgroundColor:[UIColor clearColor]];
[pageNumberLabel setFont:[UIFont fontWithName:#"MarkerFelt-Thin" size:18]];
[ourScrollView addSubview:pageNumberLabel];
[pageNumberLabel release];
NSArray *description = [NSArray arrayWithObjects:#"wow", #"bam", #"pow", #"zing", #"bling",nil];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, 250, 320, 40)];
[descriptionLabel setText:[NSString stringWithFormat:#"%#", description]];
[descriptionLabel setTextAlignment:UITextAlignmentLeft];
[descriptionLabel setTextColor:[UIColor whiteColor]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[ourScrollView addSubview:descriptionLabel];
[descriptionLabel release];
xOffset = xOffset + 320;
pageNumber = pageNumber + 1;
You're setting the descriptionLabel with a format of #"%d", which is a decimal format. What is "description"? If it's a NSString then you should be using #"%#".

iPhone dynamic addition of UILabel overlap

I am parsing the XML file through URL and displaying the values as label dynamically.
If values are changed in xml file, the new values of labels are overlapped on old values
How should i fix it
In viewDidLoad
for(int i=0;i<[appDelegate.books count]; i++){
[self createLabel:labelname labelFrame:CGRectMake(LXCOOR, LYCOOR,LWIDTH,LHEIGHT)];
}
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
label1 =[[UILabel alloc] initWithFrame:aFrame];
//label.font=[UIFont fontWithName:#"Helvetica" size:TEXTSIZE];
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
[sv addSubview:label1];
[self.view addSubview:sv];
}
remove the sv before adding it to the view...
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
if ([[sv subviews] count] > 0)
{
for(int i=0 ; i< [[sv subviews] count] ; i++)
{
UIView *subView = [[sv subviews] objectAtIndex:0];
[subView removeFromSuperview];
i++;
}
}
[sv removeFromSuperView];
label1 =[[UILabel alloc] initWithFrame:aFrame];
//label.font=[UIFont fontWithName:#"Helvetica" size:TEXTSIZE];
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
[sv addSubview:label1];
[self.view addSubview:sv];
}
In viewDidLoad add,
[self.view addSubview:sv];
Then modify the "createLabel" function to
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
for(UIView *tempView in [sv subviews]) {
[subView removeFromSuperview];
}
label1 =[[UILabel alloc] initWithFrame:aFrame];
//label.font=[UIFont fontWithName:#"Helvetica" size:TEXTSIZE];
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
[sv addSubview:label1];
}
as mentioned above, you shouldn't recreate the label over and over. Instead you should create one instance of it in viewDidLoad (or something that loads once)
-(void)viewDidLoad { ...
//code
CGRect aFrame = CGRectMake(x, y, width, height);
label1 =[[UILabel alloc] initWithFrame:aFrame];
[sv addSubview:label1];
[self.view addSubview:sv];
}
next you should call createLabel where you want to, for instance a button click or a scroll gesture. This should be your new createLabel without adding a subview
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
}

UIImage imageWithData

I have a UITable and when I click on a row I load a set of images.
I create the images with
UIImage *image = [UIImage imageWithData:data];
The problem is that ever time I click on the same row, the images are re-allocated into memory, like if no cache it is used and no memory is freed, so the allocated memory keep increasing. (I see this using the Inspector on both the device and the emulator)
I am releasing everything and also, with clang, everything looks fine.
The point is that after a while my app crashes.
Any suggestion or idea?
EDIT:
Here is the portion of code I think is generating the issue:
UIView *main = [[[UIView alloc] initWithFrame:rectScrollView] autorelease];
for (int i=0; i<[contentArray count]; i++) {
float targetWidth = rectScrollView.size.width;
float targetHeight = rectScrollView.size.height;
Link *tmp_link = [contentArray objectAtIndex:i];
AsyncImageView* asyncImage = nil;
if(tmp_link!=nil){
CGRect imageFrame = CGRectMake(targetWidth*i, 0, targetWidth, targetHeight);
//asyncImage = [[[AsyncImageView alloc] initWithFrame:imageFrame ] autorelease];
asyncImage = [[AsyncImageView alloc] initWithFrame:imageFrame ];
asyncImage.contentMode = UIViewContentModeScaleAspectFit;
NSURL* url = [NSURL URLWithString:[[IMG_SERVER_URL stringByAppendingString: tmp_link.url] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
[asyncImage loadImageFromURL:url];
[self.scrollView addSubview:(UIImageView *)asyncImage];
[asyncImage release];
}
if ([tmp_link.caption length] > 0 && asyncImage!=nil) {
float marginTop = 25;
UILabel *caption = [[UILabel alloc] init];
caption.text = tmp_link.caption;
caption.textAlignment = UITextAlignmentCenter;
caption.textColor = [UIColor whiteColor];
caption.font = [UIFont systemFontOfSize:12];
caption.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.4];
CGRect captionFrame = CGRectMake((i * targetWidth), marginTop+10, targetWidth, 20.0f);
caption.frame = captionFrame;
[self.scrollView addSubview:caption];
[caption release];
}
//[asyncImage release];
}
[main addSubview:scrollView];
[scrollView release];
if (pageControlEnabledTop) {
rectPageControl = CGRectMake(0, 5, scrollWidth, 15);
}
else if (pageControlEnabledBottom) {
//rectPageControl = CGRectMake(0, (scrollHeight - 25), scrollWidth, 15);
rectPageControl = CGRectMake(0, (scrollHeight), scrollWidth, 15);
}
if (pageControlEnabledTop || pageControlEnabledBottom) {
//pageControl = [[[UIPageControl alloc] initWithFrame:rectPageControl] autorelease];
pageControl = [[UIPageControl alloc] initWithFrame:rectPageControl];
pageControl.numberOfPages = [contentArray count];
pageControl.currentPage = page;
[main addSubview:pageControl];
[pageControl release];
}
if (pageControlEnabledTop || pageControlEnabledBottom || rememberPosition) self.scrollView.delegate = self;
//if (margin) [margin release];
return (UIScrollView *)main;
And it is used like this:
reviewImagesImage = [[IGUIScrollViewImage alloc] init];
if([currentReview.linkScreenshots count]>0){
//reviewImages
reviewImages = [[UIView alloc] init];
//placing the images
CGRect frameImages = reviewImages.frame;
frameImages.origin.x = 10;
frameImages.origin.y = (infoView.frame.origin.y + infoView.frame.size.height + 10.0f);
frameImages.size.width = 300;
frameImages.size.height = 350.0f;
reviewImages.frame = frameImages;
reviewImages.backgroundColor = [UIColor clearColor];
[reviewImagesImage setContentArray:currentReview.linkScreenshots];
[reviewImagesImage setWidth:reviewImages.frame.size.width andHeight: reviewImages.frame.size.height];
[reviewImagesImage enablePageControlOnBottom];
[reviewImagesImage setBackGroudColor: [UIColor clearColor]];
UIScrollView *tmp_sv = [reviewImagesImage getWithPosition:0];
[reviewImages addSubview:tmp_sv];
[self.view addSubview:reviewImages];
[reviewImages release];
}
I have noticed that if I try to release reviewImagesImage my app crashes :/
Of course no cache is used because new UIImage will be created from the data every time. If memory usage significantly increased like you have said, it is most likely that it is our fault that unintentionally keep the reference retained somewhere.
Once you've set your image with imageWithData, save the image to the filepath in the app bundle.

Rotate UIPickerView, Possible?

Is there a way to rotate an UIPickerView?
//example code placed in view did Load
[super viewDidLoad];
myPicker.delegate = self;
myPicker.dataSource = self;
myPicker.showsSelectionIndicator =YES;
myPicker.backgroundColor = [UIColor blackColor];
CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
//original settings
//rotate = CGAffineTransformScale(rotate, 0.1, 0.8);
rotate = CGAffineTransformScale(rotate, 0.2, 1.65);
[self.myPicker setTransform:rotate];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:#"155", #"165", #"175",
#"185", #"195", #"205", #"215", nil];
self.pickerData = array;
[array release];
NSInteger TOTALITEM = [pickerData count];
UILabel *theview[TOTALITEM-1]; // + 1 for the nil
for (int i=1;i<=TOTALITEM-1;i++) {
theview[i] = [[UILabel alloc] init];
theview[i].text = [NSString stringWithFormat:#"%d",i];
theview[i].textColor = [UIColor blackColor];
theview[i].frame = CGRectMake(0,0, 25, 25);
theview[i].backgroundColor = [UIColor clearColor];
theview[i].textAlignment = UITextAlignmentCenter;
theview[i].shadowColor = [UIColor whiteColor];
theview[i].shadowOffset = CGSizeMake(-1,-1);
theview[i].adjustsFontSizeToFitWidth = NO;
UIFont *myFont = [UIFont fontWithName:#"Helvetica" size:30];
[theview[i] setFont:myFont];
}
CGAffineTransform rotateItem = CGAffineTransformMakeRotation(-3.14/2);
rotateItem = CGAffineTransformScale(rotateItem, .5, 5);
for (int j=1;j<=TOTALITEM-1;j++) {
theview[j].transform = rotateItem;
}
pickerData = [[NSMutableArray alloc] init];
for (int j=1;j<=TOTALITEM-1;j++) {
[pickerData addObject:theview[j]];
}
You can rotate a UIPickerView by applying a transform to a view containing it, much like I suggest in my answer here for scaling the picker.
Are you looking for - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated?
Or do you mean that you want to rotate the whole control 90 degrees, thus having wheels that spin to the left and right?