dequeueReusableCellWithIdentifier返回nil使用故事板静态单元格

我有这么一个时间。 使用故事板,我创build了一个静态单元格的表视图控制器,其中包含一个UITextField允许用户input。 当用户完成时,我想要检索文本字段的内容。

这是我做的:

  • 创build了名为SingleLineFieldTableViewCellUITableViewCell的子类
  • 添加了IBOutlet UITextField *textField; 到子类并将其声明为一个属性(非primefaces,保留)并合成它。
  • 添加了IBOutlet SingleLineFieldTableViewCell *cellNamed; 到拥有的表视图控制器,并将其声明为属性(非primefaces,保留)并合成它。

  • 在故事板中,我有一个静态单元格的表格视图控制器。 其中一个单元格是自定义单元格,它被声明为SingleLineFieldTableViewCell并拥有一个UITextField 。 它也被分配一个小区标识符。

  • 我将表视图单元格和文本字段的引用连接到上面列出的相应IBOutlet。

当我运行时, dequeueReusableCellWithIdentifier返回nil 。 我认为,根据转换到故事板发行说明 ,使用Xcode 4和故事板dequeueReusableCellWithIdentifier ,“ dequeueReusableCellWithIdentifier:方法保证返回一个单元格(假定您已经定义了一个具有给定标识符的单元格)”。

奇怪的部分是,当我运行在Simulatior中,表格按预期出现(节,单元格大小等),除了我不能编辑自定义单元格。

我不知所措 任何帮助或想法?

– 约翰

我知道这个问题是一年前的事,但我今天自己就是经历了这个问题。

我认为这里的问题是使用静态单元格。

请参阅Apple文档: http : //developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

基本上这个想法是,如果你正在使用静态单元,那么就没有必要使用了

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

数据源的方法,而不是只生成所有的UI元素(静态单元格内)出口,并直接设置它们。

如果您需要使用此方法,则必须将表格更改为dynamic内容模式。

你正在iOS 5或4的build设吗?

如果使用4.x进行尝试,它不会因方法有效而崩溃,但不会返回单元格。 我没有问题设置自定义类。 这是我的整个方法:

 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ GameDetailCell* cell=[tableView dequeueReusableCellWithIdentifier:@"gameCell"]; [self configureCell:cell atIndexPath:indexPath]; return cell; } 

我的故事板看起来像: 细胞的故事板

我也是一个新手,所以这可能是完全废话,但我会告诉UITextLabel调用我的方法之一,当用户已经完成编辑,而不是担心试图从表视图:

 - (IBAction)userFinishedEditing:(id)sender { ... } - (void) someMethod { ... UITextLabel *label = ...; [label addTarget:self action:@selector(userFinishedEditing:sender:) forControlEvents: UIControlEventEditingDidEnd]; ... } 

根据苹果的文档(用数据填充静态表视图) http://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/ TP40007451-CH6-SW31

注意:如果故事板中的表视图是静态的,则包含表视图的UITableViewController的自定义子类不应该实现数据源协议。 相反,表视图控制器应该使用其viewDidLoad方法来填充表视图的数据。

因此,您只需从View Controller中删除所有Table View数据源方法。


可选的:

但是,如果您的View Controller也是其他dynamic表视图的数据源,并且仍然需要这些方法,则可以只调用静态Table View的super对应的数据源方法:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Static Table View if (tableView == self.tableView) return [super numberOfSectionsInTableView:tableView]; // Dynamic Table View // ... } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Static Table View if (tableView == self.tableView) return [super tableView:tableView numberOfRowsInSection:section]; // Dynamic Table View // ... } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Static Table View if (tableView == self.tableView) return [super tableView:tableView cellForRowAtIndexPath:indexPath]; // Dynamic Table View // ... } 
  Alert.m Class in which we used custom cell.. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"mycell"; AlertCustomCell *cell = (AlertCustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell=[[[AlertCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.lAlert.text=[[alertArray objectAtIndex:indexPath.section] objectForKey:@"alertName"]; cell.lblDate.text=[[alertArray objectAtIndex:indexPath.section] objectForKey:@"date"]; UIImageView*imgview=[[UIImageView alloc]initWithFrame:CGRectMake(-28, 0, 275, 60)]; imgview.image=[UIImage imageNamed:@"strip_s14.png" ]; UIImageView*selimgview=[[UIImageView alloc]initWithFrame:CGRectMake(-28, 0, 275, 60)]; selimgview.image=[UIImage imageNamed:@"strip_s14_h.png" ]; [cell setSelectedBackgroundView:selimgview]; cell.backgroundView = imgview; cell.backgroundColor=[UIColor clearColor]; return cell; } AlertCustomCell.m #import "AlertCustomCell.h" @implementation AlertCustomCell @synthesize lblAlert,lblDate; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { Alert=[[UITextField alloc]initWithFrame:CGRectMake(10, 18, 80, 21)]; Alert.backgroundColor=[UIColor clearColor]; Alert.text=@"Alert 1"; Alert.font=[UIFont fontWithName:@"Arial-BoldMT" size:15.0]; [self.contentView addSubview:lblAlert]; lblDate=[[UILabel alloc]initWithFrame:CGRectMake(70, 18, 150, 21)]; lblDate.backgroundColor=[UIColor clearColor]; lblDate.text=@"july 12,2011 4:17 PM"; lblDate.font=[UIFont fontWithName:@"ArialMT" size:15.0]; [self.contentView addSubview:lblDate]; } return self; }