2012-03-03 67 views
0

我对IOS开发相当陌生,所以我很抱歉如果这是一个无知的问题。打开ARC后UITableViewCell.contentView错误

重构我的代码使用ARC之后,我开始变得时执行以下线,被抛出SIGABRT错误:

UILabel *label = (UILabel *)[cell.contentView viewWithTag:labelTag]; 

控制台输出包括以下内容:

“ NSInvalidArgumentException”的,原因是: ' - [__ NSArrayM内容查看]:无法识别的选择发送到实例0x1acba0'

该行的上下文是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    const NSInteger labelTag = 1; 
    const NSInteger textFieldTag = 2; 

    UITableViewCell *cell = nil; 

    if([indexPath section] == 0) { 
     if([indexPath row] == 0) { 
      //this is the username row. Set the label accordingly. 

      cell = [[TextFieldCellController alloc] init]; 

      UILabel *label = (UILabel *)[cell.contentView viewWithTag:labelTag]; 
      label.text = @"Username"; 
     } 
     else if([indexPath row] == 1) { 
      //this is the row that contains the password input. Set the label and 
      //change the textfield to use a password input style. 

      cell = [[TextFieldCellController alloc] init]; 

      UILabel *label = (UILabel *)[cell.contentView viewWithTag:labelTag]; 
      label.text = @"Password"; 

      UITextField *text = (UITextField *)[cell.contentView viewWithTag:textFieldTag]; 
      text.secureTextEntry = YES; 


     } 
    } 
    return cell; 
} 

我在控制台窗口中玩耍,当在该行上放置断点后键入“print cell.contentView”时,我得到“没有名为contentView的成员”。我不确定这是否有帮助。

在此先感谢您的帮助。

编辑:

对不起,TextFieldCellController是加载包含一个标签和文本字段笔尖的UITableViewCell的子类。

@implementation TextFieldCellController 

@synthesize label; 
@synthesize textField; 

- (id)init { 
    self = (TextFieldCellController *)[[NSBundle mainBundle] loadNibNamed:@"TextFieldCell" owner:self options:nil]; 
    return self; 
} 
+0

消息告诉你'cell'指向'__NSArrayM'类的对象,它是'NSArray'接口的私有实现。所以让我们看看' - [TextFieldCellController init]'方法。 – 2012-03-03 05:06:11

+0

TextFieldCellController这是什么??,而且你没有正确地创建单元格。请看这个链接http://stackoverflow.com/questions/9494281/how-is-the-code-for-uploading-the-parsed-data-到了-的tableview/9494402#9494402。 – Kamarshad 2012-03-03 05:07:27

+0

我已经更新了最初的问题来描述子类。 – Leedrick 2012-03-03 07:29:27

回答

0

那不是如何实施cellForRowAtIndexPath。你为什么不打电话给dequeueReusableCellWithIdentifier:

此外(也许更重要),您不能通过调用alloc init来创建表格视图单元格。指定的初始化程序是initWithStyle:reuseIdentifier:;我相信你必须叫它。

最后,我非常怀疑这个TextFieldCellController。如果它是一个UITableViewCell子类,那么像这样初始化它不会导致它具有任何具有1或2标签的子视图。如果它不是UITableViewCell子类,那么究竟是什么?我希望你没有使用UIViewController从nib加载单元格;那将是非常错误的。

+0

我以为dequeueReusableCellWithIdentifier是可选的?我花了一段时间试图找出如何有一个可重用的自定义单元格。这是工作,直到我启用ARC。你能告诉我更好的方法吗?我已经更新了最初的问题。 – Leedrick 2012-03-03 07:28:47