2011-11-16 86 views
0

我有一個從數組加載的tableview,由於某種原因,它重新使用了單元格,但第二次在表格下方加載了相同的信息。UITableViewCell正在被重用,但我無法解決原因?

http://screencast.com/t/Ig8bcqSpLzp

上面的視頻應該給你明白我的意思的想法。

這是我的代碼加載電池:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 
     accessicon *current =[arryAccess objectAtIndex:indexPath.row] ; 
     cell.textLabel.text = current.text; 
     cell.imageView.image = [UIImage imageNamed: current.iconPath]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero]; 
     cell.accessoryView = switchView; 
     switchView.myValue = current.iconValue; 

     if(totalIconValue & current.iconValue) { 
      [switchView setOn: YES animated:NO]; 
     } else { 
      [switchView setOn: NO animated:NO]; 

     } 

     [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
     [switchView release]; 


     return cell; 


} 

任何幫助將不勝感激

編輯:我已經在實際設備上進行更新。但是我上面的代碼,它仍然選擇項目它不應該 - 不再重複使用名稱,但如果我上下滾動它自己選擇它們?

在這裏看到視頻:http://screencast.com/t/a9N1qbws

湯姆

回答

3

此代碼:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
if (cell == nil) { 
    ... 

是根本錯誤的。它應該是:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    ... 

然後,任何行特定的應該在if語句之外。

這種模式,一般應該是:

  • 出隊可重複使用的電池
  • ,如果你沒有得到一個:
    • 分配一個新的細胞
    • 執行一般小區設置即,設置適用於所有單元格
  • 設置特定行的單元格並返回它。

希望這會有所幫助。

+0

感謝您花時間爲我解釋,真的很感激。 – TMB87

+0

編輯;我已經上傳了上面的代碼並添加了一個新視頻,它現在正在做其他奇怪的事情!你能看看嗎? – TMB87

+0

這很奇怪,但並不完全不尋常。這表示重新使用的單元沒有正確重新初始化。看看你如何分配交換對象和管理這些對象。您不應該每次都需要分配一個。一旦一個單元格擁有一個,你就不需要總是創建一個單元格。開關創建和目標選擇器設置,以及單元格選擇樣式都可以在塊內完成;這是所有通用的一次性設置。你也可以通過不重複地獲得性能提升。 –

1

您正在不斷地創造,而不是重複使用舊的新行。

招行

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

if塊,端移動一切你在它外面的,如果塊有內,除了線

mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero]; 
cell.accessoryView = switchView; 

你只需要創建開關一次爲行,當它被初始化時(也就是說,在if語句中)。我建議你創建它併爲它分配一個標籤,以便稍後可以通過它的標籤訪問它。您的代碼現在每次使用單元時都會創建一個新的交換機,這可能是交換機以這種方式運行的原因。

+0

我已經完成了這個工作,但是現在在滾動時它會自動地自動調整它自己的位置!我已更新視頻/代碼 – TMB87

+0

我編輯了我的答案,我希望它有幫助。 –

2

您並未重複使用您的單元格。只有在init失敗時才填充單元格。試試這個代碼:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

accessicon *current =[arryAccess objectAtIndex:indexPath.row] ; 
cell.textLabel.text = current.text; 
cell.imageView.image = [UIImage imageNamed: current.iconPath]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
mySwitch *switchView = [[mySwitch alloc] initWithFrame:CGRectZero]; 
cell.accessoryView = switchView; 
switchView.myValue = current.iconValue; 

if(totalIconValue & current.iconValue) { 
    [switchView setOn: YES animated:YES]; 
} else { 
    [switchView setOn: NO animated:YES]; 

} 

[switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
[switchView release]; 

或者在ios5下開發並擺脫alloc和init。

+0

您可以將現有的代碼升級到iOS5嗎?還是必須重新啓動? – TMB87

+0

您必須加載最新的SDK並將基本SDK設置爲iOS5。如果您不必爲4.3之前的iOS開發,我還建議更改爲ARC。 – dasdom

相關問題