UICell problem iPhone while Scrolling tableView - iphone

My Current Problem Related links are one of below.
http://www.freeimagehosting.net/image.php?a65dae5f4a.png>http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png alt="Free Image Hosting by FreeImageHosting.net">
[url=http://www.freeimagehosting.net/image.php?a65dae5f4a.png][img]http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png[/img][/url]
[url=http://www.freeimagehosting.net/image.php?a65dae5f4a.png][img=http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png][/url]
http://www.freeimagehosting.net/>http://www.freeimagehosting.net/uploads/a65dae5f4a.png border=0 alt="Free Image Hosting">
[url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/a65dae5f4a.png[/img][/url]
[url=http://www.freeimagehosting.net/][img=http://www.freeimagehosting.net/uploads/a65dae5f4a.png][/url]
I have implemented following code, to my Table view,
enter code here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// customizing cell
student *t=[studentsList objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor lightGrayColor];
CGRect label1Frame=CGRectMake(10, 10, 290, 25);
CGRect label2Frame=CGRectMake(10, 33, 290, 25);
CGRect label3Frame=CGRectMake(10, 56, 290, 25);
UILabel *lbltmp;
lbltmp=[[UILabel alloc] initWithFrame:label1Frame]; // name
lbltmp.font=[UIFont boldSystemFontOfSize:15];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.textColor=[UIColor blackColor];
lbltmp.text=t.stuName;
[cell.contentView addSubview:lbltmp];
[lbltmp release];
lbltmp=[[UILabel alloc] initWithFrame:label2Frame]; // roll no & address
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:#"%i - %#",t.stuNo,t.stuAddress];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
lbltmp=[[UILabel alloc] initWithFrame:label3Frame]; // city & pin
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:#"%# - %#",t.stuCity,t.stuPin];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
//----------------------------------
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
If I implement the code as described above, problem occurs as described in above images.
If I implement following code, no problem occurs.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// customizing cell
student *t=[studentsList objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor lightGrayColor];
CGRect label1Frame=CGRectMake(10, 10, 290, 25);
CGRect label2Frame=CGRectMake(10, 33, 290, 25);
CGRect label3Frame=CGRectMake(10, 56, 290, 25);
UILabel *lbltmp;
lbltmp=[[UILabel alloc] initWithFrame:label1Frame]; // name
lbltmp.font=[UIFont boldSystemFontOfSize:15];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.textColor=[UIColor blackColor];
lbltmp.text=t.stuName;
[cell.contentView addSubview:lbltmp];
[lbltmp release];
lbltmp=[[UILabel alloc] initWithFrame:label2Frame]; // roll no & address
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:#"%i - %#",t.stuNo,t.stuAddress];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
lbltmp=[[UILabel alloc] initWithFrame:label3Frame]; // city & pin
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:#"%# - %#",t.stuCity,t.stuPin];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
//----------------------------------
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
}
return cell;
}
Implementing the above code means, recreating the each table cell while user scrolls the tableView. It may slow down your application & it isn't the correct solution,
Because In my application I have thousands of students & if I create each cell while scroll My Application may hang. What Should be the solution? Help me....

I think the problem is that you're adding subviews on top of already-added subviews when the cells are recycled.
One way to fix this: subclass UITableViewCell and give it pointers to UILabels for the text you want to change. Then when the cell is recycled, you can just access those properties, instead of re-adding new subviews.
Reference: An Apple doc on table view cells.

I think the solution to your problem is as follows:
Implement following method to your tableViewCotroller
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
{
}
For example I have Implemented following
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
{
CGRect photoFrame=CGRectMake(5, 5, 60, 60);
CGRect label1Frame=CGRectMake(90, 10, 290, 25);
CGRect label2Frame=CGRectMake(90, 30, 290, 25);
CGRect label3Frame=CGRectMake(90, 50, 290, 25);
UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 300, 80) reuseIdentifier:cellIdentifier] autorelease];
UILabel *tmp;
tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.tag=1;
tmp.textColor=[UIColor blackColor];
tmp.font=[UIFont boldSystemFontOfSize:15];
[cell.contentView addSubview:tmp];
[tmp release];
tmp=[[UILabel alloc] initWithFrame:label2Frame];
tmp.tag=2;
tmp.adjustsFontSizeToFitWidth=0;
tmp.textColor=[UIColor grayColor];
tmp.font=[UIFont systemFontOfSize:13];
[cell.contentView addSubview:tmp];
[tmp release];
tmp=[[UILabel alloc] initWithFrame:label3Frame];
tmp.tag=3;
tmp.adjustsFontSizeToFitWidth=0;
tmp.textColor=[UIColor grayColor];
tmp.font=[UIFont systemFontOfSize:13];
[cell.contentView addSubview:tmp];
[tmp release];
UIImageView *imgView=[[UIImageView alloc]initWithFrame:photoFrame];
imgView.tag=4;
[cell.contentView addSubview:imgView];
[imgView release];
return cell;
}
I have also called above method form cellForRowAtIndexPath
example
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [self getCellContentView:CellIdentifier];
}
// customizing cell
student *t=[studentsList objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor lightGrayColor];
UILabel *lb1=(UILabel*)[cell viewWithTag:1];
UILabel *lb2=(UILabel*)[cell viewWithTag:2];
UILabel *lb3=(UILabel*)[cell viewWithTag:3];
NSString *dtlOne=[NSString stringWithFormat:#"Enr.No - %i, %#",t.stuNo,t.stuAddress];
NSString *dtlTwo=[NSString stringWithFormat:#"%# - %#.",t.stuCity,t.stuPin];
lb1.text=t.stuName;
lb2.text=dtlOne;
lb3.text=dtlTwo;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
That works perfectly, and I can also use my custom content view in my cell.

Related

App crash when scroll on UiTableViewCell

Hi my App goes crash on scrolling tableview and mentain a crash log name with LowMemory.
Can anyone tell me how can i manage this. Code which i ma using is here :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSLog(#" %i",indexPath.section);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell=nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *cellBg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 90)];
cellBg.image=[UIImage imageNamed:#"Cellbg.png"];
[cell addSubview:cellBg];
UILabel *lblRecipeBorder=[[UILabel alloc]initWithFrame:CGRectMake(105, 5, 640, 80)];
lblRecipeBorder.backgroundColor=[UIColor blackColor];
lblRecipeBorder.layer.borderWidth=5.0;
lblRecipeBorder.layer.cornerRadius=8.0;
lblRecipeBorder.layer.borderColor=[[UIColor darkGrayColor] CGColor];
[cell addSubview:lblRecipeBorder];
UIImageView *RecipeeImg=[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100, 80)];
RecipeeImg.backgroundColor=[UIColor clearColor];
RecipeeImg.layer.borderWidth=5.0;
RecipeeImg.layer.cornerRadius=8.0;
RecipeeImg.layer.borderColor=[[UIColor darkGrayColor] CGColor];
RecipeeImg.image=[UIImage imageNamed:[[appdelegate.arrFiderTool objectAtIndex:indexPath.row] objectForKey:#"RecipestepPics"]];
RecipeeImg.layer.masksToBounds = YES;
[cell addSubview:RecipeeImg];
UILabel *lblRecipee=[[UILabel alloc]initWithFrame:CGRectMake(140, 10, 620, 70)];
lblRecipee.backgroundColor=[UIColor clearColor];
[lblRecipee setNumberOfLines:2];
lblRecipee.textColor=[UIColor colorWithRed:255.0/255.0 green:52.0/255.0 blue:179.0/255.0 alpha:1.0];
//lblRecipee.font=[UIFont boldSystemFontOfSize:18.0];
lblRecipee.font=[UIFont fontWithName:#"Noteworthy-Bold" size:18.0];
//[lblMeetingId setFont:[UIFont fontWithName:#"Times New Roman" size:14]];
lblRecipee.text=[[appdelegate.arrFiderTool objectAtIndex:indexPath.row] objectForKey:#"RecipeName"];
[cell addSubview:lblRecipee];
[RecipeeImg release];
[lblRecipee release];
[cellBg release];
[lblRecipeBorder release];
// }
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
You should be reusing the labels and image views to add the cell. You can do this by subclassing the UITableViewCell or by giving the views tags. In my opinion subclassing is the best way, but for now I will you an example of the tagging views:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
NSLog(#" %i",indexPath.section);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIImageView *recipeImageView = nil;
UILabel *recipeLabel = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIImageView *cellBg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 90)];
cellBg.image = [UIImage imageNamed:#"Cellbg.png"];
[cell addSubview:cellBg];
UILabel *lblRecipeBorder=[[UILabel alloc]initWithFrame:CGRectMake(105, 5, 640, 80)];
lblRecipeBorder.backgroundColor=[UIColor blackColor];
lblRecipeBorder.layer.borderWidth=5.0;
lblRecipeBorder.layer.cornerRadius=8.0;
lblRecipeBorder.layer.borderColor=[[UIColor darkGrayColor] CGColor];
[cell addSubview:lblRecipeBorder];
UIImageView *recipeImageView =[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100, 80)];
recipeImageView.backgroundColor=[UIColor clearColor];
recipeImageView.layer.borderWidth=5.0;
recipeImageView.layer.cornerRadius=8.0;
recipeImageView.layer.borderColor=[[UIColor darkGrayColor] CGColor];
recipeImageView.layer.masksToBounds = YES;
recipeImageView.tag = 456;
[cell addSubview:recipeImageView];
UILabel *recipeLabel=[[UILabel alloc]initWithFrame:CGRectMake(140, 10, 620, 70)];
recipeLabel.backgroundColor=[UIColor clearColor];
[recipeLabel setNumberOfLines:2];
recipeLabel.textColor=[UIColor colorWithRed:255.0/255.0 green:52.0/255.0 blue:179.0/255.0 alpha:1.0];
recipeLabel.font=[UIFont fontWithName:#"Noteworthy-Bold" size:18.0];
recipeLabel.tag = 457;
[cell recipeLabel];
[recipeImageView release];
[recipeLabel release];
[cellBg release];
[lblRecipeBorder release];
} else {
recipeImageView = [cell viewWithTag:456];
recipeLabel = [cell viewWithTag:457];
}
NSDictionary *item = [appdelegate.arrFiderTool objectAtIndex:indexPath.row];
recipeImageView.image = [UIImage imageNamed:[item objectForKey:#"RecipestepPics"]];
recipeLabel.text = [item objectForKey:#"RecipeName"];
return cell;
}
Change:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
To:
UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

How to insert a customized row below a row that is tapped

I am developing a project in which there is one requirement that in a table view when I tapped a row there should be one row added below that row and also I want that row to be customized as I want to add a text view in that row.
Please tell me how can I do this.
Any suggestions would be highly appreciated.
My current code:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];`
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
UIImageView *imgViewBackground=(UIImageView *)[cell.contentView viewWithTag:20];
UIImageView *imgView=(UIImageView *)[cell.contentView viewWithTag:21];
UILabel *label1=(UILabel *)[cell.contentView viewWithTag:22];
UILabel *label2=(UILabel *)[cell.contentView viewWithTag:23];
UILabel *label3=(UILabel *)[cell.contentView viewWithTag:24];
[imgViewBackground setImage:[UIImage imageNamed:#"bba3.png"]
[imgView setImage:[UIImage imageNamed:#"picdefault.png"]];
[label1 setText:[self.usernameArray objectAtIndex:indexPath.row]];
[label2 setText:[NSString stringWithFormat:#"%#",[self.level1Array objectAtIndex:indexPath.row]]];
[label3 setText:[self.ageArray objectAtIndex:indexPath.row]]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if ([identifier isEqualToString:#"Cell"]) {
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 300, 98)];
imageView.tag=20;
[imageView setImage:[UIImage imageNamed:#"bba2.png"]];
[cell.contentView addSubview:imageView];
[imageView release];
UIImageView *imagePicView=[[UIImageView alloc]initWithFrame:CGRectMake(25, 20, 60, 60)];
imagePicView.tag=21;
[cell.contentView addSubview:imagePicView];
[imagePicView release];
UILabel *namelabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 20, 200, 20)];
namelabel.backgroundColor=[UIColor clearColor];
namelabel.font=[UIFont systemFontOfSize:15];
namelabel.tag=22;
[namelabel setTextAlignment:UITextAlignmentLeft];
[namelabel setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:namelabel];
[namelabel release];
UILabel *activitylabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 40, 200, 20)];
activitylabel.backgroundColor=[UIColor clearColor];
activitylabel.font=[UIFont italicSystemFontOfSize:14];
activitylabel.tag=23;
[activitylabel setTextAlignment:UITextAlignmentLeft];
[activitylabel setTextColor:[UIColor darkGrayColor]];
[cell.contentView addSubview:activitylabel];
[activitylabel release];
UILabel *agelabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 60, 200, 20)];
agelabel.backgroundColor=[UIColor clearColor];
agelabel.font=[UIFont systemFontOfSize:13];
agelabel.tag=24;
[agelabel setTextAlignment:UITextAlignmentLeft];
[agelabel setTextColor:[UIColor darkGrayColor]];
[cell.contentView addSubview:agelabel];
[agelabel release];
}
return cell;
}
Look at the WWDC 2011 developer video for UITableView changes and tips - in there they describe how to do exactly that. Sadly there's not been any sample code I've ever been able to find, but they document the approach pretty well in the talk.

How to set selection style as blue if each cell is added an cell back ground image in iPhone sdk?

I had a problem to set the selection style as blue, I have added the cell back ground image so that i can not set the selection style as blue how can i make it if we added images to each cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[myTableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
UIView* elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
cellBGImageView.image=[UIImage imageNamed:#"cellbg.png" ];
[elementView addSubview:cellBGImageView];
[cellBGImageView release];
Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];
UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
[nameLable setFont:[UIFont boldSystemFontOfSize:17]];
nameLable.textAlignment=UITextAlignmentLeft;
//nameLable.textColor = [UIColor blackColor];
nameLable.numberOfLines = 0;
nameLable.tag=2;
nameLable.backgroundColor = [UIColor clearColor];
nameLable.text=objectForCustomer.customerName;
[elementView addSubview:nameLable];
[nameLable release];
return cell;
}
I know that when you have a custom cell, you can point it in IB. Don't really know if it is possible from code (I guess it should).
Try using this in your code:
[cell setBackgroundView:cellBGImageView.image];
I am sceptic about if it works, but worth a try.
When you are covering entire cell with image, you cannot see the cell selected.
you have to implement custom cell for that to handle (or show) that some cell is selected.
Hey I have sen a problem in your code that you are setting the selectionStyle of cell as none.
The problrm is not with the image but the problem is with the code.
I am rewriting the code for you convinience. Please check it out
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = (UITableViewCell )[myTableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
UIView elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//THIS IS THE LINE I HAVE COMMENTED THAT WAS CREATING THE PROBLEM
// cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
cellBGImageView.image=[UIImage imageNamed:#"cellbg.png" ];
[elementView addSubview:cellBGImageView];
[cellBGImageView release];
Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];
UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
[nameLable setFont:[UIFont boldSystemFontOfSize:17]];
nameLable.textAlignment=UITextAlignmentLeft; //nameLable.textColor = [UIColor blackColor]; nameLable.numberOfLines = 0;
nameLable.tag=2;
nameLable.backgroundColor = [UIColor clearColor];
nameLable.text=objectForCustomer.customerName;
[elementView addSubview:nameLable];
[nameLable release];
return cell;
}
hAPPY cODING...

Cell selection style in iPhone

By default there is three selection style in iPhone - table view.
gray - blue - none.
I don't need gray or blue.
I want to set my custom.
For example, in normal situation a cell should have "aaaa.png" background, and selected cell should have "bbbbb.png" background.
I have tried to apply cell.backgroundView & cell.selectedBackground view. However it isn't working.
Three lines of code here, but you don't need an image!
UIView *selectedBackgroundViewForCell = [UIView new];
[selectedBackgroundViewForCell setBackgroundColor:[UIColor redColor]];
theCell.selectedBackgroundView = selectedBackgroundViewForCell;
Here's one way to do it with only 1 line of code:
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pink.png"]] autorelease];
obviously you'll need to make the image.
I tried following & it worked.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
XML_FitnessPrograms *t=[arrayPrograms objectAtIndex:indexPath.row];
cell=((indexPath.row%2)==0) ?
[self getCellContentView:CellIdentifier program_name:t.program_name alterNate:NO indexPath:indexPath] :
[self getCellContentView:CellIdentifier program_name:t.program_name alterNate:YES indexPath:indexPath] ;
CGRect a=CGRectMake(8, 0, 300, 44);
UIImageView *aImg=[[UIImageView alloc] initWithFrame:a];
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
aImg.image=[UIImage imageNamed:#"white-rect-tab.png"]; //arrow.png
bImg.image=[UIImage imageNamed:#"white-rect-tab-copy.png"];
[aImg setContentMode:UIViewContentModeScaleToFill];
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.backgroundView=aImg;
cell.selectedBackgroundView=bImg;
[aImg release];
[bImg release];
}
return cell;
}
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier program_name:(NSString*)program_name alterNate:(BOOL)alterNate indexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell; UILabel *tmp;
CGRect label1Frame=CGRectMake(5, 0, 260, 44);
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.frame=CGRectMake(8, 0, 280, 44);
cell.backgroundColor=[UIColor clearColor];
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:#"Arial-BoldMT" size:15]];
tmp.text=program_name;
[tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:tmp]; [tmp release];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
return cell;
}

iphone sdk: Can't reuse cell with viewWithTag (can not setText to reused labels)

This is my cellForRowAtIndexPath function. I could not get the setText to the label to work. Can you please help me out?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *messageLabel = nil;
int row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, ROWHEIGHT) reuseIdentifier:CellIdentifier];
messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 240, 60)];
[messageLabel setFont:[UIFont fontWithName:#"ArialMT" size:12]];
[messageLabel setTextColor:[UIColor blackColor]];
[messageLabel setBackgroundColor:[UIColor clearColor]];
[messageLabel setNumberOfLines:3];
[messageLabel setLineBreakMode:UILineBreakModeWordWrap];
[messageLabel setTag: messageTag];
[cell.contentView addSubview:messageLabel];
}
else{
messageLabel = (UILabel *)[cell viewWithTag:messageTag];
}
[messageLabel setText:[[[aSingleton wallItemArray] objectAtIndex:row] message]];
NSLog(#" -- > at row %d, message: %#", row, [[[aSingleton wallItemArray] objectAtIndex:row] message]);
return cell;
}
You're adding the UILabel to the cell's contentView, but asking the cell for the view.
Have you tried:
messageLabel = (UILabel *)[cell.contentView viewWithTag:messageTag];
EDIT: Also, you have two memory leaks - you're alloc'ing a UITableViewCell and a UILabel without (auto)releasing them anywhere.