2012-02-22 95 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指出的,它不会帮助您在单元格上设置标签。在您创建的视图上设置标签。 (虽然我怀疑这是你的崩溃)。

相关问题