Theo mình, khi sử dụng UITableView bạn nên reuse cell, đây là một tính năng rất hay của UITableView, cho phép dùng lại các cell mà không cần phải khởi tạo lại mỗi lần nữa. Khi bạn scroll table mà thấy bị đè lên nhau hay text đậm dần lên thì đó là do quản lý cell chưa tốt. Đây là một ví dụ của mình với hàm cellForRowAtIndexPath:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *textLabel;
UIImageView* imgView;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"] autorelease];
cell.selected = UITableViewCellSelectionStyleNone;
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopping_cell_bg.png"]];
UIImageView *selectView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shopping_cell_select.png"]];
cell.backgroundView = bgView;
cell.selectedBackgroundView = selectView;
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(48, 7, 240, 30)];
[textLabel setTag:7749];
[textLabel setBackgroundColor:[UIColor clearColor]];
[textLabel setFont:[UIFont boldSystemFontOfSize:17]];
[textLabel setTextColor:[UIColor blackColor]];
[cell addSubview:textLabel];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 32, 32)];
imgView.backgroundColor = [UIColor clearColor];
[imgView setTag:8864];
[cell addSubview:imgView];
[textLabel release];
[bgView release];
[selectView release];
[imgView release];
}
textLabel = (UILabel*)[cell viewWithTag:7749];
imgView = (UIImageView*)[cell viewWithTag:8864];
switch (indexPath.row) {
case 0:
textLabel.text = SetupLanguage(kStrShoppingFind);
[imgView setImage:[UIImage imageNamed:@"ic_shopping_1.png"]];
break;
case 1:
textLabel.text = SetupLanguage(kStrShoppingAboutMe);
[imgView setImage:[UIImage imageNamed:@"ic_shopping_2.png"]];
break;
case 2:
textLabel.text = SetupLanguage(kStrShoppingPurchase);
[imgView setImage:[UIImage imageNamed:@"ic_shopping_3.png"]];
break;
case 3:
textLabel.text = SetupLanguage(kStrShoppingCoupon);
[imgView setImage:[UIImage imageNamed:@"ic_shopping_3.png"]];
break;
case 4:
textLabel.text = SetupLanguage(kStrShoppingWork);
[imgView setImage:[UIImage imageNamed:@"ic_shopping_5.png"]];
break;
default:
break;
}
return cell;
}