Reputation: 19303
I've subclassed UITableViewCell
to show numbers from 1 to 70.
In every cell i'm checking the winning numbers and chance their background. The problem is that after a few scrolls the tableview turns really slow to unusable. I don't understand why because to my understanding I am reusing the cells. Maybe it's because I'm creating 70 UITextFields each time?
Please advise
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
...
...
...
SubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (cell == nil) {
cell = [[SubCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
}
for(int i=0;i<7;i++)
{
for(int j=0;j<10;j++)
{
UITextField *tempField = [[UITextField alloc]initWithFrame:CGRectMake(2+(j*31), 4+(i*28), 30, 27)];
[tempField setBorderStyle:UITextBorderStyleNone];
[tempField setOpaque:YES];
[tempField setTextAlignment:NSTextAlignmentCenter];
[tempField setTextColor:[UIColor whiteColor]];
[tempField setUserInteractionEnabled:NO];
tempField.text = [NSString stringWithFormat:@"%d",i*10+j+1];
if([[cell.currentWinningArray objectAtIndex:i*10+j] isEqualToString:@"0"])
{
[tempField setBackground:[UIImage imageNamed:@"blue"]];
}
else
[tempField setBackground:[UIImage imageNamed:@"orange"]];
[cell.contentView addSubview:tempField];
}
}
return cell;
}
Upvotes: 1
Views: 225
Reputation: 726879
The reason your cells become slow on reuse is that you keep adding subviews to them even after reusing a cell. Each time a cell gets created or reused, you add seventy subviews to it, but you never remove the subviews. You do not see the "old" subviews from the reused cells because newly added subviews sit on top of them, but the old subviews are still there.
You need to change your code to create and add subviews only once, when the cell gets allocated. When the cell gets reused, you should change the values inside the views, but keep the views themselves unchanged.
One way to achieve this would be giving each tempField
a tag between 0 and 69, like this:
SubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (cell == nil) {
cell = [[SubCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
for(int i=0;i<7;i++)
{
for(int j=0;j<10;j++)
{
UITextField *tempField = [[UITextField alloc]initWithFrame:CGRectMake(2+(j*31), 4+(i*28), 30, 27)];
[tempField setBorderStyle:UITextBorderStyleNone];
[tempField setOpaque:YES];
[tempField setTextAlignment:NSTextAlignmentCenter];
[tempField setTextColor:[UIColor whiteColor]];
[tempField setUserInteractionEnabled:NO];
[tempField setTag:10*i+j];
[cell.contentView addSubview:tempField];
}
}
}
for(int i=0;i<7;i++)
{
for(int j=0;j<10;j++)
{
UITextField *tempField = [cell subviewWithTag:10*i+j];
tempField.text = [NSString stringWithFormat:@"%d",i*10+j+1];
if([[cell.currentWinningArray objectAtIndex:i*10+j] isEqualToString:@"0"])
{
[tempField setBackground:[UIImage imageNamed:@"blue"]];
}
else
{
[tempField setBackground:[UIImage imageNamed:@"orange"]];
}
}
}
return cell;
Upvotes: 3