I have implemented following code in my application.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%#%i",searchQueryString,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
XML_SearchResult *t=[arrayResults objectAtIndex:indexPath.row];
cell=((indexPath.row%2)==0) ?
[self getCellContentView:CellIdentifier drillTitle:t.drill_title stroke_type:t.stroke_type exercise_type:t.exercise_type level_name:t.level_name age_range:t.age_range alterNate:NO] :
[self getCellContentView:CellIdentifier drillTitle:t.drill_title stroke_type:t.stroke_type exercise_type:t.exercise_type level_name:t.level_name age_range:t.age_range alterNate:YES] ;
CGRect a=[cell frame];
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
bImg.image=[UIImage imageNamed:#"white-rect-tab-3.png"];
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.selectedBackgroundView=bImg; [bImg release];
}
return cell;
}
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier drillTitle:(NSString*)drillTitle stroke_type:(NSString*)stroke_type exercise_type:(NSString*)exercise_type level_name:(NSString*)level_name age_range:(NSString*)age_range alterNate:(BOOL)alterNate
{
CGRect label1Frame=CGRectMake(5, 20, 260, 32),label2Frame=CGRectMake(5, 62, 120, 30),label3Frame=CGRectMake(130, 62, 170, 30);//,BgFrame=CGRectMake(0, 0, 320, 60);
UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 312, 95) reuseIdentifier:cellIdentifier] autorelease];
UILabel *tmp; UIImageView *imgBg;
cell.backgroundColor=[UIColor clearColor];
// drill title label
tmp=[[UILabel alloc] initWithFrame:CGRectMake(5, 1, 260, 32)];
tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:#"ArialRoundedMTBold" size:18]]; tmp.text=drillTitle; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// title label - tip
tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:#"ArialRoundedMTBold" size:18]]; tmp.text=stroke_type; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// sub title - exercise type label - tip
tmp=[[UILabel alloc] initWithFrame:CGRectMake(5, 40 , 260, 32)];
tmp.textColor=[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:#"ArialRoundedMTBold" size:15]]; tmp.text=exercise_type; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// Age - Range
tmp=[[UILabel alloc] initWithFrame:label2Frame]; tmp.adjustsFontSizeToFitWidth=0; tmp.text=[NSString stringWithFormat:#"Age Range : %#",age_range];
tmp.textColor= [UIColor blackColor];//[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];// : [UIColor blackColor] ;
[tmp setBackgroundColor:[UIColor clearColor]];
[tmp setFont:[UIFont fontWithName:#"ArialMT" size:14]]; [tmp setShadowOffset:CGSizeMake(1,1)]; [tmp setShadowColor:[UIColor lightGrayColor]];
[cell.contentView addSubview:tmp]; [tmp release];
// Difficulty leve - Range
tmp=[[UILabel alloc] initWithFrame:label3Frame]; tmp.adjustsFontSizeToFitWidth=0; tmp.text=[NSString stringWithFormat:#"| Diff. Level : %#",level_name];
tmp.textColor=[UIColor blackColor];//[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];
[tmp setShadowColor:[UIColor lightGrayColor]];
[tmp setBackgroundColor:[UIColor clearColor]];
[tmp setFont:[UIFont fontWithName:#"ArialMT" size:14]]; [tmp setShadowOffset:CGSizeMake(1,1)];
[cell.contentView addSubview:tmp]; [tmp release];
// arrow
imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(280, 35, 13, 13)]; imgBg.contentMode=UIViewContentModeScaleToFill;
imgBg.image=[UIImage imageNamed:#"arrow-2.png"];
[cell.contentView addSubview:imgBg]; [cell.contentView sendSubviewToBack:imgBg]; [imgBg release];
// line
imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(-4, 94, 298, 2)]; imgBg.contentMode=UIViewContentModeScaleToFill;
imgBg.image=[UIImage imageNamed:#"line.png"];
[cell.contentView addSubview:imgBg]; [cell.contentView sendSubviewToBack:imgBg]; [imgBg release];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
return cell;
}
That's all about customizing cell dynamically.
But the problem is - first time only visible cells are created, then after - user tries to scroll down. other cells which are going to be visible is going to be created & then displayed. Due to this, there is some delay in scrolling tableview at first time.
What is the solution for this ?
When developing for mobile platforms, you need to be concerned about memory footprints. I feel that if you tried to load 400 cells with images all at once, you might be asking for trouble. It's not the recommended method and you will certainly introduce a huge delay in loading this.
Instead of hacking like this, work in improving the performance of your cellForIndexPath function to make it load quicker. Or asynchronously.
It is probably the image loading which takes all the time. Creating cells should not take very long, especially if you reuse old ones.
If the images are often the same, then you could cache those. Also scaling takes time and also if the images are not PNG they will take longer to load.
But the solution for me has been to not load the images when I create the cell, i.e. NOT in:
tableView:cellForRowAtIndexPath:
but instead implement:
scrollViewDidEndDecelerating
This is a delegate function which is called when the table stops scrolling. Then you can get a list of the visible cells (indexPathsForVisibleRows) and set the image on each of those.
Related
So I have setup my UILabel, but when I set the text in my game loop to a string that is the score (so the Text is reset every loop) my app crashes.
This is the error I get:
0x15560b0: cmpl (%eax), %ecx Thread 1
The Breakpoint says this:
EXC_BAD_ACCESS(code=1, address=0x67b30064
Here is how I am setting up my UILabel (in my init method) :
//SET UP THE SCORE LABEL HERE
scoreLabel = [[UILabel alloc] init];
scoreString = [NSString stringWithFormat:#"%d", score];
[scoreLabel setFont: [UIFont fontWithName: #"TimesNewRoman" size: 10.0f]];
[scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
[scoreLabel setBackgroundColor:[UIColor clearColor]];
[scoreLabel setTextColor: [UIColor clearColor]];
[scoreLabel setTextColor: [UIColor whiteColor]];
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview: scoreLabel];
//[scoreLabel setText: scoreString];
Thank You!
Are you doing your label update off the main queue? I had these crashes until I dispatched the update on the main queue.
dispatch_async(dispatch_get_main_queue(), ^{
self.lblSyncProgress.text = #"Some string";
});
//in your .h file
UILabel *scoreLabel;
NSString *scoreString;
//in your .m file
//SET UP THE SCORE LABEL HERE
scoreLabel = [[UILabel alloc] init];
[scoreLabel setFont: [UIFont fontWithName: #"TimesNewRoman" size: 10.0f]];
[scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
[scoreLabel setBackgroundColor:[UIColor clearColor]];
[scoreLabel setTextColor: [UIColor clearColor]];
[scoreLabel setTextColor: [UIColor whiteColor]];
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview: scoreLabel];
//In your update method
scoreString = [NSString stringWithFormat:#"%d", score];
scoreLabel.text = scoreString;
I noticed in your code that you had
scoreString = [NSString stringWithFormat:#"%d", score];
Make sure that "score" is a char. If it is not have a look at this
I am new to this iphone development .I am now stuck with a problem .In my application i require coverflow in which multiple labels should be shown.So for that i added an image as a sub view for view and then added the labels as a sub view for the image view.Every thing works fine for the first 7 elements But when i scroll it is showing the same elements again
I am using
https://github.com/nicklockwood/iCarousel
this to attain coverflow
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIImageView *imageView =nil;
UILabel *yourLabel=nil;
UILabel *yourLabel1=nil;
UILabel *yourLabel2=nil;
cardAvailableDataObject *singleCard=[self.cards objectAtIndex:index];
if (view == nil)
{
//set up reflection view
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0,0,200,self.view.frame.size.width/1.42)] autorelease];
imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0,0,200,self.view.frame.size.width/1.42)];
[view addSubview:imageView];
yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -5, 200, 50)];
yourLabel.textAlignment = UITextAlignmentCenter;
[yourLabel setTextColor:[UIColor colorWithRed:(38/255.f) green:(171/255.f) blue:(226/255.f) alpha:1.0f]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 17.0f]];
[imageView addSubview:yourLabel];
yourLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(90, 40, 300, 20)];
[yourLabel1 setTextColor:[UIColor blackColor]];
[yourLabel1 setBackgroundColor:[UIColor clearColor]];
[yourLabel1 setFont:[UIFont fontWithName: #"Trebuchet MS" size: 12.0f]];
[imageView addSubview:yourLabel1];
yourLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(90, 61, 300, 20)];
[yourLabel2 setTextColor:[UIColor blackColor]];
[yourLabel2 setBackgroundColor:[UIColor clearColor]];
[yourLabel2 setFont:[UIFont fontWithName: #"Trebuchet MS" size: 12.0f]];
[imageView addSubview:yourLabel2];
}
else
{
imageView = [[view subviews] lastObject];
yourLabel = [[imageView subviews] lastObject];
yourLabel1 = [[imageView subviews] lastObject];
yourLabel2= [[imageView subviews] lastObject];
}
yourLabel.text=singleCard.name;
yourLabel2.text=singleCard.companyname;
yourLabel1.text=singleCard.designation;
imageView.image=[profile objectAtIndex:index];
return view;
}
This is my code .Can any one suggest how to solve this
I am developing an universal(iphone/ipad) application. I am using uitableview in this application. table cell being draw using code. Now problem is that it can be fit either on iPad or iPhone from single code, If I put code only on single time.
To achieve this I need to put condition to check the device and then need to code 2 time, mean 1 for iphone and another for iPad to set its background images and frames of uilable etc..
This is looking rubbish and stupid job to me. Is there any way to achieve this goal by single code.
My code is as below. I am using xcode 4.2 ARC
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIFont *fontName=[UIFont fontWithName:#"HelveticaNeue LT 57 Cn" size:18.0f];
UIFont *fontTitle=[UIFont fontWithName:#"HelveticaNeue LT 57 Cn" size:12.0f];
UIFont *grayLight=[UIFont systemFontOfSize:12.0f];
//UIFont *fontBold=[UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSMutableDictionary *dic=[arrEmails objectAtIndex:indexPath.row];
//NSLog(#"%#",dic);
int tot=4;
NSString *imageName=[NSString stringWithFormat:#"inbox_list%d",(indexPath.row%tot)+1];
UIImageView *imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(-30, 10, 303, 70)];
[imgBg setImage:[UIImage imageNamed:imageName]];
[cell.contentView addSubview:imgBg];
NSString *name=nameDisplayType==1 ? [[dic valueForKey:#"firstName"] stringByAppendingFormat:#" %#", [dic valueForKey:#"lastName"]] : [[dic valueForKey:#"lastName"] stringByAppendingFormat:#" %#", [dic valueForKey:#"firstName"]];
UILabel *lblFirstName=[[UILabel alloc] initWithFrame:CGRectMake(30 ,15,150,20)];
[lblFirstName setText:name];
[lblFirstName setBackgroundColor:[UIColor clearColor]];
[lblFirstName setTextColor:UIColorFromRedGreenBlue(162, 23, 25)];
[lblFirstName setTextAlignment:UITextAlignmentLeft];
[lblFirstName setFont:fontName];
[lblFirstName setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:lblFirstName];
UILabel *lblEmailId=[[UILabel alloc] initWithFrame:CGRectMake(30,28,200,20)];
[lblEmailId setText:[dic valueForKey:#"emailId"]];
[lblEmailId setBackgroundColor:[UIColor clearColor]];
[lblEmailId setTextAlignment:UITextAlignmentLeft];
[lblEmailId setFont:grayLight];
[lblEmailId setTextColor:[UIColor grayColor]];
[lblEmailId setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:lblEmailId];
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date=[formatter dateFromString:[dic valueForKey:#"date"]];
[formatter setDateFormat:#"dd MMM, yyyy"];
NSString *formattedDate=[formatter stringFromDate:date];
//NSLog(#"%# formated:%#",[dic valueForKey:#"date"],formattedDate);
UILabel *lblDate=[[UILabel alloc] initWithFrame:CGRectMake(190,15,100,20)];
[lblDate setText:formattedDate];
[lblDate setBackgroundColor:[UIColor clearColor]];
[lblDate setTextAlignment:UITextAlignmentLeft];
[lblDate setFont:grayLight];
[lblDate setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:lblDate];
UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(20,53,200,20)];
[lblTitle setText:[dic valueForKey:#"title"]];
[lblTitle setBackgroundColor:[UIColor clearColor]];
[lblTitle setTextAlignment:UITextAlignmentLeft];
[lblTitle setFont:fontTitle];
[lblTitle setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:lblTitle];
UIButton *btnViewGift=[[UIButton alloc] initWithFrame:CGRectMake(235, 55, 20, 20)];
[btnViewGift addTarget:self action:#selector(btnViewGiftPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnViewGift setTag:indexPath.row];
[cell.contentView addSubview:btnViewGift];
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
Please help and give me some ideas. Thanks in advance.
Instead of passing hardcoded agruments in frame, you can decide the frame dynamically using frame and bounds property of view. But you have to take care of each UI element.
I have created customize UIView with following code .....it's running with no issue....but when i check for Memory leaks...it shows leaks at point where i have made comment ...this is subclass of Uiview.
Please help me...
-(id) initWithLabel:(NSString*)labelName{
self = [super init];
if (self) {
self.layer.cornerRadius=8.0f;
self.layer.borderColor=[UIColor whiteColor].CGColor;
self.layer.borderWidth=2.0f;
/*memory leaks 28.6% */ [self setBackgroundColor:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]];
[self setFrame:CGRectMake(86, 168, 160, 77)];
[self setAlpha:1];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 28, 75, 21)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentLeft];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont fontWithName:#"Helvetica" size:17]];//memory leak 57%
[label setText:labelName];
[label setOpaque:NO];//memory leak
[self addSubview:label];
[label release];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(108, 22, 37, 37)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator setBackgroundColor:[UIColor clearColor]];
[activityIndicator setHidesWhenStopped:YES];
[self addSubview:activityIndicator];
[activityIndicator release];
}
return self;
}
try this
UIColor *myColor = [[UIColor alloc] initWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1];
[self setBackgroundColor:myColor];
[myColor release];
Hey man that is not memory leaks. that is autoreleased objects.so dont worry about them.apple wont reject urs.the condition is if u use alloc,init,new or retain then only u have responsibility to release.so leave it.otherwise do like above by alloc and init then release it .that'st it
i am trying to upload 8-10 thumbnail images on UIButton. The images are loaded directly via Links to my server for eg. www.abc.com/fire.png. I have placed these UIButtons inisde a Scrollview. the following code is how i achieve it. but whenever this method gets called my applications hangs up as i guess the images take time to load on the buttons. How can i stop my app from getting stuck for few seconds??
- (void)loadSCRView
{
[self.view addSubview:scrollView];
for (i = 0; i < [myEngine.logoUrlArr count]; i++)
{
NSString *imageName = [NSString stringWithFormat:#"%#",[myEngine.logoUrlArr objectAtIndex:i]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:imageName] options:NSDataReadingUncached error:nil]];
UIButton *btn = [[UIButton alloc]init];
[btn setImage:image forState:UIControlStateNormal];
[btn setFrame:CGRectMake(x, 4, 57, 57)];
x=x+ 70.00;
btn.tag = i;
[scrollView addSubview:btn];
[btn addTarget:self action:#selector(click:) forControlEvents:UIControlEventTouchDown];
UILabel *label = [[UILabel alloc]init];
label.text = [myEngine.nameArr objectAtIndex:i];
[label setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[label setFrame:CGRectMake(btn.frame.origin.x , 59, 70, 20)];
//[label setFrame:CGRectMake(100 , 59, 70, 20)];
y=y+ 100.00;
[scrollView addSubview:label];
[btn release];
[label release];
}
You should try to fetch these images (preferably in a background thread) before the scroll view is displayed. Save these on the disk & use them in the scroll view. A network operation will generally not be that fast that you don't observe a lag.
HTH,
Akshay
To avoid freezing you have to go with NSOperationQueue. Here is a good and appropriate example...
You can use threads so that download the image in the background thread & cache it.So that next time your performance will be good.