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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8131311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:25:43  来源:igfitidea点击:

dequeueReusableCellWithIdentifier returns nil using storyboard static cells

iphoneiosxcodeuitableviewstoryboard

提问by johnnyspo

I am having a heck of time with this. Using storyboard, I created a table view controller with a static cell that contains a UITextField to allow for user input. When the user is finished, I want to retrieve the contents of the text field.

我有很多时间来处理这个。使用故事板,我创建了一个带有静态单元格的表视图控制器,该单元格包含一个 UITextField 以允许用户输入。当用户完成后,我想检索文本字段的内容。

Here is what I did:

这是我所做的:

  • Created a subclass of UITableViewCellnamed SingleLineFieldTableViewCell
  • Added IBOutlet UITextField *textField;to the subclass and declared it as a property (nonatomic, retain) and synthesized it.
  • Added IBOutlet SingleLineFieldTableViewCell *cellNamed;to the owning table view controller, and declared it as a property (nonatomic, retain) and synthesized it.

  • In storyboard, I have a table view controller with static cells. One of the cells is the custom cell, which is declared as SingleLineFieldTableViewCelland owns a UITextField. It is also assigned a cell identifier.

  • I attached referencing outlets of the table view cell and the text field to the appropriate IBOutlets listed above.
  • 创建了一个UITableViewCell名为的子类SingleLineFieldTableViewCell
  • 添加IBOutlet UITextField *textField;到子类并将其声明为属性(非原子、保留)并合成它。
  • 添加IBOutlet SingleLineFieldTableViewCell *cellNamed;到拥有的表视图控制器,并将其声明为属性(非原子,保留)并合成它。

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

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

When I run, dequeueReusableCellWithIdentifierreturns nil. I thought that with Xcode 4 and storyboards, dequeueReusableCellWithIdentifier, according to Converting to Storyboards Release Notes, "The dequeueReusableCellWithIdentifier:method is guaranteed to return a cell (provided that you have defined a cell with the given identifier)".

当我运行时,dequeueReusableCellWithIdentifier返回nil. 我认为对于 Xcode 4 和故事板,dequeueReusableCellWithIdentifier根据Converting to Storyboards Release Notes,“该dequeueReusableCellWithIdentifier:方法保证返回一个单元格(前提是您已经定义了一个具有给定标识符的单元格)”。

The weird part is that when I run in the Simulatior, the table appears as expected (section, cell size, etc.), except that I can't edit the custom cell.

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

I'm at a loss. Any help or ideas?

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

--John

- 约翰

回答by Ants

I know this question was a year ago, but I just went through this problem myself today.

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

I think the problem here is using static cells.

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

See the Apple docs: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

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

Basically the idea is that if you are using static cells then there is no need to use

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

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

data source method, instead just generate outlets to all your UI elements (inside the static cells) and set them directly.

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

If you need to use this method then you must change the table to dynamic content mode.

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

回答by utahwithak

Are you building for iOS 5 or 4?

您是为 iOS 5 还是 4 构建的?

If you are trying with 4.x it won't crash as the method is valid, but doesn't return the cell. I have had no problem setting it up with custom classes. here is my entire method:

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

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

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

my storyboard looks like: Storyboard for cell

我的故事板看起来像: 单元格的故事板

回答by trojanfoe

I am a newbie too, so this might be complete crap, but I would have told the UITextLabelto call one of my methods when the user had finished editing and not worried about trying to deque it from the Table View:

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

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

- (void) someMethod
{
    ...

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

    ...
}

回答by Dmytro Strelbytskyi

According to the Apple's docs (Populating a Static Table View With Data) http://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW31

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

Note:If a table view in a storyboard is static, the custom subclass of UITableViewControllerthat contains the table view should not implement the data source protocol. Instead, the table view controller should use its viewDidLoadmethod to populate the table view's data.

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

Thus you just need to remove all the Table View data source methods from your View Controller.

因此,您只需要从您的视图控制器中删除所有表视图数据源方法。



Optional:

可选的:

However if your View Controller is also the data source for other dynamic Table View(s) and you still need these methods, it's possible to call only the super's corresponding data source methods for the static Table View:

但是,如果您的 View Controller 也是其他动态 Table View 的数据源,并且您仍然需要这些方法,则可以仅为super静态 Table View调用相应的数据源方法:

- (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
    // ...
}

回答by alok chauve

    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;
}