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 开发者_JAVA百科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.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ReuseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *label1;
UILabel *label2;
UILabel *label3;
if( cell == nil )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,18,280,10)];
label1.font = [UIFont fontWithName:@"Helvetica" size:14];
label1.tag = 1;
[cell.contentView addSubview:label1];
[label1 release];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(10,25,280,10)];
label2.font = [UIFont fontWithName:@"Helvetica" size:14];
label2.tag = 2;
[cell.contentView addSubview:label2];
[label2 release];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(10,40,290,11)];
label3.font = [UIFont fontWithName:@"Helvetica" size:14];
label3.tag = 3;
[cell.contentView addSubview:label3];
[label3 release];
}
else
{
// retrieve the labels using the tags you defined earlier
label1 = (UILabel *)[cell.contentView viewWithTag:1];
label2 = (UILabel *)[cell.contentView viewWithTag:2];
label3 = (UILabel *)[cell.contentView viewWithTag:3];
}
if ( [indexPath row] < [itemsArray count] )
{
NSDictionary *item = [itemsArray objectAtIndex:[indexPath row]];
label1.text = [NSString stringWithFormat:@"Description %@", [item valueForKey:@"Desc"]];
label2.text = [NSString stringWithFormat:@"Price %@", [item valueForKey:@"Price"]];
label3.text = [NSString stringWithFormat:@"Loc %@", [item valueForKey:@"Loc"]];
}
return cell;
}
精彩评论