2012-02-22 98 views
0

我正在嘗試使用標籤和單元標識符來重新使用cellViews,但是當單元格被重新使用時,下面的代碼會崩潰。我想我快到了。任何人都可以看到錯誤?當單元格被「回收」時,重用UITableView單元會導致崩潰

// Customize the appearance of table view cells. 
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    const NSInteger BUTTON_TAG = 1001; 
    const NSInteger SWITCH_TAG = 1002; 
    const NSInteger TEXTFIELD_TAG = 1003; 

    NSString *CellIdentifier = @""; 
    if(indexPath.section == 2 && indexPath.row == 0) 
     CellIdentifier = @"Button"; 
    else if (indexPath.section == 3) 
     CellIdentifier = @"Switch"; 
    else 
     CellIdentifier = @"TextField"; 


    UISwitch *switchView; 
    UITextField *textField; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     if (CellIdentifier == @"TextField") 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      CGRect frame = CGRectInset([cell.contentView bounds], 70, 10); 
      textField = [[[UITextField alloc] initWithFrame:frame] autorelease]; 
      textField.keyboardType = UIKeyboardTypeDefault; 
      textField.returnKeyType = UIReturnKeyDone; 
      textField.autocorrectionType = UITextAutocorrectionTypeNo; 
      textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      textField.delegate = self; 

      cell.accessoryView = textField; 
      cell.tag = TEXTFIELD_TAG; 

     } 
     else if (CellIdentifier == @"Button") 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 
      cell.clipsToBounds=YES; 
      cell.tag = BUTTON_TAG; 
     } 
     else if (CellIdentifier == @"Switch") 
     { 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      switchView = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease]; 

      cell.accessoryView = switchView; 
      cell.tag = SWITCH_TAG; 
     } 
    } 
    else 
    { 
     textField = (UITextField*)[cell viewWithTag:TEXTFIELD_TAG]; 
     switchView = (UISwitch*)[cell viewWithTag:SWITCH_TAG];  
    } 

崩潰日誌

2012-02-22 14:50:08.352 ***[2304:207] -[UITableViewCell setSecureTextEntry:]: unrecognized selector sent to instance 0x6368270 
2012-02-22 14:50:08.355 ***[2304:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setSecureTextEntry:]: unrecognized selector sent to instance 0x6368270' 
+0

取消註釋// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];'。沒有? – 2012-02-22 14:38:04

+0

爲什麼'cell = [[[UITableViewCell alloc] ...'註釋掉了? – dasblinkenlight 2012-02-22 14:40:03

+0

我已經取消註釋了。只是測試中的錯誤 – JonWells 2012-02-22 14:40:37

回答

0

你不說飛機墜毀是什麼,或者提供一個回溯,但一個問題,我馬上看到的是,你總是在做:

switchView = (UISwitch*)[cell viewWithTag:SWITCH_TAG];  

所有回收的細胞,即使只有三之一類型有一個switchView。

您也只爲一種細胞設置了TEXTFIELD_TAG,但在訪問所有類型的「回收」細胞時也參考了它。

編輯添加:我看到你已經從你的控制檯添加了例外。撥打setSecureTextEntry時會引發例外情況。我沒有看到setSecureTextEntry代碼中的任何地方,您複製&粘貼到問題中,所以我建議您在實際代碼中尋找setSecureTextEntry,並確定它是接收該呼叫的UITextField,而不是UITableViewCell(其中可以是安全的UITextField居住的超級視圖)。

0

旁註釋細胞ALLOC線你設置.TAG性質的細胞,而不是文本字段和交換機,這是最有可能崩潰的原因。同時發佈崩潰日誌,以便我們看到究竟是什麼導致應用程序崩潰。

0

開始加入你的新控件的內容查看(或小區)爲子視圖,就像這樣:

​​

...等等的其他意見。看看是否修復了崩潰。

此外,正如@Eugene指出的,它不會幫助您在單元格上設置標籤。在您創建的視圖上設置標籤。 (雖然我懷疑這是你的崩潰)。

相關問題