定制table view cell, 里面包含一个UILabel, 一个 UITextField,当鼠标点击textfield要准备输入的时候,程序就crash, 报告一下信息:[NSObject respondsToSelector]: message sent to deallocated.code 大致如下:
定制的cell:
ComboCell.h @interace ComboCell: UITableViewCell <UITextFieldDelegate>
{
UITextField * textField;
UILabel *name;
}
@property (strong, nonatomic) IBOutlet UITextField * textField;
@property (strong, nonatomic) IBOutlet UILabel * textField;
@endComboCell.m
...
-(BOOL) textFieldShouldReturn:(UITextField *) textField
{
 [textField resignFirstResponder];
return YES.
}
还有一个 comboCell.xib, 里面就是在 UITableViewCell里面添加le label and textField, 并且把textField的delegate设定为 combocell.使用这个cell的文件里主要有下面的东东:-(void)viewDidLoad

  [super  viewDidLoad];
  
[self.tableview registerNib:[UINibnibWithNibName:@ComboCell"bundle"nil]forCellResuseIdentifier:@ComboCellId"];
}...
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
   static NSString * cellID = @"ComboCellId";
ComboCell * cell = (ComboCell *)[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexpath];
return cell;
}我注意到的一个现象是:如果我不把textField的delegate设定为 ComboCell, 就不会出现crash的现象, 似乎也跟我用nib来创建table cell 有关。 希望高手指点是什么原因导致crash,怎样fix ? 我有很多不同的cell需要定制,我不想把textField的delegate指定为tableview 的controller.想让每一个模块都相对独立。
多谢大侠帮忙!

解决方案 »

  1.   

    if (cell == nil)
    {
         //给CELL分配一个内存并初始化
    }
      

  2.   

    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
    {
       static NSString * cellID = @"ComboCellId";
    ComboCell * cell = (ComboCell *)[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexpath];
    if(cell == nil)
    {
       cell = [ComboCell alloc] init......]; 方法自己视情况而定
    }
    return cell;
    }
      

  3.   

      大家说得都不对。 其实在使用 nib来构造对象的时候,在ios6 里面已经不需要做  cell = nil 的判断了。仔细看  apple的文档就明白其中的原因了。我自己fix掉了这个问题。
    在nib里面去掉textField的delegate的制定,
    override  function awakeFromNib ()
    在这里指定 textField的delegate.{
     textField.delegate = self;
    }然后就OK 了。依然感谢你们的回答。