Ios cellForRowAtIndexPath在滚动几下后变慢
我将Ios cellForRowAtIndexPath在滚动几下后变慢,ios,objective-c,cocoa-touch,uitableview,Ios,Objective C,Cocoa Touch,Uitableview,我将UITableViewCell子类化,以显示从1到70的数字。 在每一间牢房里,我都在检查中奖号码,并猜测他们的背景。问题是,在滚动几下之后,tableview会变得非常缓慢,无法使用。我不明白为什么,因为据我所知,我是在重复使用细胞。也许是因为我每次都要创建70个UITextFields 请告知 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)i
UITableViewCell
子类化,以显示从1到70的数字。
在每一间牢房里,我都在检查中奖号码,并猜测他们的背景。问题是,在滚动几下之后,tableview会变得非常缓慢,无法使用。我不明白为什么,因为据我所知,我是在重复使用细胞。也许是因为我每次都要创建70个UITextFields
请告知
- (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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
...
...
...
子单元格*单元格=[tableView dequeueReusableCellWithIdentifier:@“myCell”];
如果(单元格==nil){
cell=[[SubCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“myCell”];
}
对于(int i=0;i单元格重用速度变慢的原因是,即使在重用单元格后,您也会不断向其添加子视图。每次创建或重用单元格时,您都会向其添加70个子视图,但从未删除子视图。您看不到“旧的”来自重用单元的子视图,因为新添加的子视图位于它们之上,但旧的子视图仍然存在
在分配单元格时,您只需更改代码即可创建和添加子视图一次。当重新使用单元格时,您应更改视图中的值,但保持视图本身不变
实现这一点的一种方法是给每个tempField
一个介于0和69之间的标记,如下所示:
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;
SubCell*cell=[tableView dequeueReusableCellWithIdentifier:@“myCell”];
如果(单元格==nil){
cell=[[SubCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“myCell”];
对于(int i=0;i很有意义。谢谢。你能告诉我我的单元格从不为零的原因是什么吗?它永远不会进入第一个if.@Sha这是。经过一些阅读后,我了解到,因为我使用的是原型单元格,所以我不能使用allcell==nil
。一个简短的答案解释了这一点:。回答很好,并引出@dasblinkenlight。谢谢!@Sha不客气!如果您使用的是原型单元格,那么您最好直观地放入所有子视图,为它们分配标记,然后删除If(cell==nil)
分支中的代码。直观地说,您指的是IB?我不想在Xcode:)中拖动70行代码。)