tags:

views:

744

answers:

2
+1  A: 

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.

Tyler
A: 

I think the solution of given problem is following,

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.

sugar