1
我已將子類型UITableViewCell
顯示1到70之間的數字。 在每個單元格中,我都會檢查獲勝的數字並有機會獲得他們的背景。問題是,幾次滾動後,tableview變得非常慢,無法使用。我不明白爲什麼,因爲根據我的理解,我正在重複使用這些單元格。也許是因爲我每次創建70個UITextFields?cellForRowAtIndexPath在幾個滾動後變緩慢
請告知
- (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;
}
使有很大的意義。謝謝。你能告訴我,我的牢房從來沒有零的原因是什麼?它永遠不會進入第一個如果。 – Segev
@Sha這是[可能是因爲你正在使用故事板,並給你的'UITableViewCell'標識符''myCell「'](http://stackoverflow.com/a/11788323/335858)。 – dasblinkenlight
經過一些閱讀後,我已經瞭解到,因爲我使用的是原型單元格,所以我無法使用所有'cell == nil'。一個簡短的答案解釋它:http://stackoverflow.com/a/9461518/1578927。很好的答案,並帶領@dasblinkenlight。謝謝! – Segev