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

时间:2011-11-15 03:58:17

标签: iphone ios xcode uitableview storyboard

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

这是我做的:

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

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

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

当我跑步时,dequeueReusableCellWithIdentifier会返回nil。根据{{​​3}},我认为使用Xcode 4和故事板dequeueReusableCellWithIdentifier,“dequeueReusableCellWithIdentifier:方法保证返回一个单元格(前提是您已定义了具有给定标识符的单元格) ”。

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

我很茫然。任何帮助或想法?

- 约翰

5 个答案:

答案 0 :(得分:6)

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

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

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

基本上,我们的想法是,如果您使用静态单元格,则无需使用

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

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

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

答案 1 :(得分:3)

您是否正在为iOS 5或4构建?

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

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

    GameDetailCell* cell=[tableView dequeueReusableCellWithIdentifier:@"gameCell"];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

我的故事板看起来像: Storyboard for cell

答案 2 :(得分:0)

我也是新手,所以这可能是完全废话,但我会告诉UITextLabel在用户完成编辑后调用我的方法之一并且不担心尝试从表中解除它视图:

- (IBAction)userFinishedEditing:(id)sender
{
    ...
}

- (void) someMethod
{
    ...

    UITextLabel *label = ...;
    [label addTarget:self action:@selector(userFinishedEditing:sender:) forControlEvents: 
UIControlEventEditingDidEnd];

    ...
}

答案 3 :(得分:0)

根据Apple的文档(使用数据填充静态表视图)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也是其他动态表视图的数据源,并且您仍然需要这些方法,则可以仅为静态表视图调用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
    // ...
}

答案 4 :(得分:-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;
}