dequeueReusableCellWithIdentifier使用storyboard静态单元格返回nil

joh*_*spo 9 iphone xcode storyboard uitableview ios

我对此感到很沮丧.使用storyboard,我创建了一个带有静态单元格的表格视图控制器,该静态单元格包含允许用户输入的UITextField.用户完成后,我想检索文本字段的内容.

这是我做的:

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

  • 在storyboard中,我有一个带有静态单元格的表视图控制器.其中一个单元格是自定义单元格,它被声明为SingleLineFieldTableViewCell拥有一个UITextField.它还被分配了一个小区标识符.

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

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

奇怪的是,当我在Simulatior中运行时,表格按预期显示(部分,单元格大小等),除了我无法编辑自定义单元格.

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

- 约翰

Ant*_*nts 6

我知道这个问题是一年前的事,但我今天自己就解决了这个问题.

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

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

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

如果需要使用此方法,则必须将表更改为动态内容模式.


alo*_*uve -2

    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;
}
Run Code Online (Sandbox Code Playgroud)