2013-07-14 78 views
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; 
    } 

回答

3

的原因,你的細胞成爲重用慢是你保持甚至重用細胞後加入子視圖他們。每次單元格被創建或重用時,都會爲其添加七十個子視圖,但您絕不會刪除子視圖。您不會看到重複使用的單元格中的「舊」子視圖,因爲新添加的子視圖位於其上,但舊子視圖仍然存在。

當單元格被分配時,您需要更改代碼以創建和添加子視圖一次。當單元格被重用時,您應該更改視圖內的值,但保持視圖本身不變。

一種方式來實現,這將是給每個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; 
+0

使有很大的意義。謝謝。你能告訴我,我的牢房從來沒有零的原因是什麼?它永遠不會進入第一個如果。 – Segev

+0

@Sha這是[可能是因爲你正在使用故事板,並給你的'UITableViewCell'標識符''myCell「'](http://stackoverflow.com/a/11788323/335858)。 – dasblinkenlight

+0

經過一些閱讀後,我已經瞭解到,因爲我使用的是原型單元格,所以我無法使用所有'cell == nil'。一個簡短的答案解釋它:http://stackoverflow.com/a/9461518/1578927。很好的答案,並帶領@dasblinkenlight。謝謝! – Segev