Ios 自定义UITableViewCell中的UITextField委托

Ios 自定义UITableViewCell中的UITextField委托,ios,objective-c,uitableview,uitextfield,Ios,Objective C,Uitableview,Uitextfield,我在我的故事板中有一个定制的单元原型,名为YesNoTableViewCell。在那个单元格中,我有UITextField 我想在自定义单元格类中使用UITextField的委托方法 这是我的密码: 霉菌控制者 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIde

我在我的故事板中有一个定制的单元原型,名为
YesNoTableViewCell
。在那个单元格中,我有
UITextField

我想在自定义单元格类中使用
UITextField
的委托方法

这是我的密码:

霉菌控制者

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
    static NSString *CellIdentifier = @"YesNoTableViewCell";
    YesNoTableViewCell *cell = [tableView
                                dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
      cell = [[YesNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier];
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
  }
是的

@interface YesNoTableViewCell : UITableViewCell <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *yesNoInput;

@end
但意识到这从来没有叫过:

(id)initWithStyle:(UITableViewCellStyle)样式 reuseIdentifier:(NSString*)reuseIdentifier

因为:

YesNoTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]

永不归零


对此有什么建议吗?

在您实现时,它不会返回单元格nil

if (cell == nil){
  cell = [[YesNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
}

所以只需删除这段代码

那么,当您在initWithStyle方法中放置断点时,它永远不会到达该方法?你的桌面视图显示了吗?cellForRowAtIndexPath:方法是否也被调用?我记得,如果tableView的高度为0,则不会调用委托方法。是的,会调用除自定义类之外的所有对象。。正如我在文章顶部所说的,实际上我想将UITextField委托方法放在那个自定义类中。您是否指定了tableView.delegate=self和tableView.datasource=self?我在故事板中连接了这两个方法。实际上,问题不在于数据没有显示,而是我的自定义类中的initWithStyle从未被调用。是的,我知道如何正确地初始化自定义类?只需使用[self.yourcollectionView注册表项nb:[UINib nibWithName:@“nibName”bundle:[NSBundle Mainbundle]]forCellReuseIdentifier:@“CustomCell”]
if (cell == nil){
  cell = [[YesNoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
}