App crash when scroll on UiTableViewCell - iphone

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];

Related

changing the position of the detailtextlabel in the cell?

I have an Iphone application in which i am combining two arrays and displaying in the same table.the problem here is i was not able to change the position of the detailed textlabel in the cell.here is my code snippet `
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"cell_with_arrow.png"]];
[cell setBackgroundView:img];
[img release];
}
if(newssarray==nil||[newssarray count]==0)
{
}
else
{
NSDictionary *dicttable=[newssarray objectAtIndex:indexPath.row];
head=[dicttable objectForKey:#"news"];
head1=[dicttable objectForKey:#"name"];
NSString *sub=[dicttable objectForKey:#"created"];
NSLog (#"%#",head);
[[cell viewWithTag:12] removeFromSuperview];
[[cell viewWithTag:13] removeFromSuperview];
[[cell viewWithTag:14] removeFromSuperview];
UIButton *shopLabel=[self buttonWithText:head andFrame:CGRectMake(49, 12, 250,5)] ;
shopLabel.tag=12;
//shopLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
[shopLabel addTarget:self action:#selector(openRestaurantsList) forControlEvents:UIControlEventTouchUpInside];
UIButton *shopLabel1=[self buttonWithText:head1 andFrame:CGRectMake(49, 12, 250,5)] ;
shopLabel1.tag=14;
//shopLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
[shopLabel1 addTarget:self action:#selector(openRestaurantsList) forControlEvents:UIControlEventTouchUpInside];
cell.detailTextLabel.frame = CGRectMake(49, 30, 150,5);
if(indexPath.row < [newsseperatearray count])
{
UIImage *cellImage = [UIImage imageNamed:#"news.png"];
cell.imageView.image = cellImage;
//NSStringEncoding encoding=NSASCIIStringEncoding;
cell.imageView.frame=CGRectMake(0,0,48,48);
cell.imageView.bounds=CGRectMake(0,0,48,48);
[cell.imageView setClipsToBounds:NO];
NSLog(#"%#",sub);
cell.detailTextLabel.text = sub;
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
else
{
UIImage *cellImage = [UIImage imageNamed:#"menu_icon.png"];
cell.imageView.image = cellImage;
//NSStringEncoding encoding=NSASCIIStringEncoding;
cell.imageView.frame=CGRectMake(0,0,48,48);
cell.imageView.bounds=CGRectMake(0,0,48,48);
[cell.imageView setClipsToBounds:NO];
}
[cell addSubview:shopLabel];
[cell addSubview:shopLabel1];
// [cell addSubview:shopLabel2];
}
return cell;
}
`can anybody help me to achieve this?
Make a UITableViewCell subclass, keep that cell style as UITableViewCellStyleSubtitle, and override layoutSubviews in that subclass. You can then change the frame of detailTextLabel (just make sure to call the super implementation of layoutSubviews in your method).

UIProgressView hides when scroll the tableview

In my iphone app i am having a button, when I click on the button, download operation starts and i am replacing the button with UIProgressView. This UIProgressView will show the download progress. But once a UIProgressView is added, when I scroll it is disappeared. Here is my code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [self getCellContentView:CellIdentifier];
cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
cell.cellItemButton = (UIButton *)[cell viewWithTag:3];
DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
NSString *url;
if([itemObj.itemStatus isEqualToString:#"Available"]){
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"img_pressed"] forState:UIControlStateHighlighted];
[cell.cellItemButton addTarget:self action:#selector(download) forControlEvents:UIControlEventTouchUpInside];
url = [NSString stringWithFormat:#"%#",itemObj.availableIcon];
}else if([itemObj.itemStatus isEqualToString:#"Downloading"]){
url = [NSString stringWithFormat:#"%#",itemObj.availableIcon];
[cell.contentView addSubview:myprogressView];
cell.cellItemButton.hidden = YES;
}
[cell.cellitemImage setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:#"item01.png"]];
cell.cellItemName.text = [NSString stringWithFormat:#"%#",itemObj.itemName];
return cell;
}
- (void)download{
UIButton *btn = (UIButton *)clickedBtnSender;
UIMenuItemCell *cell = [(UIMenuItemCell *)[[clickedBtnSender superview] superview] retain];
myprogressView = [[[CustomProgressView alloc]initWithFrame:CGRectMake(25, 140, 100, 21)] retain];
myprogressView.tag = selectedTag;
btn.hidden = YES;
for (UIProgressView *currentProgress in cell.contentView.subviews) {
if ([currentProgress isKindOfClass:[UIProgressView class]]){
[currentProgress removeFromSuperview];
}
}
[cell.contentView addSubview:myprogressView];
}
Please help.
EDIT
- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 150, 60);
CGRect imgFrame = CGRectMake(20, 48, 110, 123);
CGRect btnFrame = CGRectMake(25, 140, 100, 26);
CGRect progressFrame = CGRectMake(25, 140, 100, 21);
UIImageView *itemImg;
UIButton *itemBtn;
UIProgressView *itemProgView;
UIMenuItemCell *cell = [[UIMenuItemCell alloc] init] ;
cell.frame = CellFrame;
//Initialize ImageView
itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
itemImg.tag = 2;
[cell.contentView addSubview:itemImg];
//Initialize Button
itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
itemBtn.frame = btnFrame;
itemBtn.tag = 3;
itemBtn.titleLabel.textColor = [UIColor blueColor];
itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
[cell.contentView addSubview:itemBtn];
//Initialize ProgressView
itemProgView = [[CustomProgressView alloc]initWithFrame:progressFrame];
itemProgView.tag = 4;
//[cell.contentView addSubview:itemProgView];
return cell;
}
I think you need to have unique cellIdentifier for each cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d", indexPath.row];
UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self getCellContentView:CellIdentifier];
// Some codes here
}
return cell;
}
Additional based on you last edit
Change
UIMenuItemCell *cell = [[UIMenuItemCell alloc] init];
to
UIMenuItemCell *cell = [[UIMenuItemCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier];

uitableview - data and if (!cell)

I have a big problem with an UITableView, I want to use a label inside the cell, so I use this method for do it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {
}
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
return cell;
}
in this way I can see all the data on my table, but the label are overlap, than I try to use this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
}
else {
}
return cell;
}
the label are OK but in this case I can see on my table only 5 data, and this 5 data are repeat for some time...
For example if in the first case on my table I can see: 1,2,3,4,5,6,7,8,9,10,...
in the second case I seee: 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...
where is the problem?
add this code
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
before
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
Currently you add a label to the cell and the next time the cell is reused..the label is still there and you add a label on top of it.
The code you posted is specifying UITableViewCellStyleSubtitle as the cell style, which means each cell will a text label and detail text label available in its corresponding properties textLabel, and detailTextLabel. So there's no reason for you to allocate additional instances of UILabel. Instead, just populate the text properties of the existing labels. For example, you could rewrite your implementation like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellID = #"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
cell.textLabel.text = (indexPath.section == 0 ?
[array objectAtIndex:indexPath.row] :
#"ReadMore");
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.

UICell problem iPhone while Scrolling tableView

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.