I have a tableView where I add UILabel’s to cell contentView as subview in cellForRowAtIndexPath method like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ReuseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil ){
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Code to remove labels overlay
for(UIView* view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
if ([indexPath row]<[itemsArray count]){
UILabel* label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,18,280,10)];
label1.text = [NSString stringWithFormat:@"Description %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Desc"]];
label1.font = [UIFont fontWithName:@"Helvetica" size:14];
[cell.contentView addSubview:label1];
[label1 release];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10,25,280,10)];
label2.text = [NSString stringWithFormat:@"Price %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Price"]];
label2.font = [UIFont fontWithName:@"Helvetica" size:14];
[cell.contentView addSubview:label2];
[label2 release];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,40,290,11)];
label3.text = [NSString stringWithFormat:@"Loc %@",[[itemsArray objectAtIndex:[indexPath row]] valueForKey:@"Loc"]];
label3.font = [UIFont fontWithName:@"Helvetica" size:14];
[cell.contentView addSubview:label3];
[label3 release];
}
return cell;
}
When I scroll table view UILabels overlap on previous one’s because of cell reuse so I remove it using:
// Code to remove labels overlay
for(UIView* view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
Which is why my scrolling is not so smooth if there are many rows to display. How to make my scrolling smooth ?
Don’t remove the labels: reuse them. If you give each one a
tag, you can retrieve them using the content view’s-viewWithTag:and just change their text.