ios UITableView 中的断言失败 configureCellForDisplay:forIndexPath:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15311201/
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-08-30 22:38:08  来源:igfitidea点击:

Assertion failure in UITableView configureCellForDisplay:forIndexPath:

iosobjective-ccocoa-touchuitableview

提问by Jason Taylor

I'm not sure where the error is here, having looked at other similar issues. I received an Assertion failure.

在查看其他类似问题后,我不确定错误在哪里。我收到断言失败。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

I think it is something simple but hope someone can help.

我认为这很简单,但希望有人能提供帮助。

Below is my code:

下面是我的代码:

#import "StockMarketViewController.h"

@interface StockMarketViewController ()

@end


@implementation StockMarketViewController
@synthesize ShareNameText, ShareValueText, AmountText;
@synthesize shares, shareValues;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return [shares count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];



    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;

}

回答by vikingosegundo

you are never creating a cell, you just try to reuse a dequeued cell. but as you never created one, there is none.

你永远不会创建一个单元格,你只是尝试重用一个出列的单元格。但是因为你从来没有创造过,所以没有。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;
}

or try (only iOS 6+)

或尝试(仅限 iOS 6+)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;
}


from UITableView.h

来自 UITableView.h

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier 
                           forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

-dequeueReusableCellWithIdentifier:will always need a check, if a cell was returned, while
-dequeueReusableCellWithIdentifier:forIndexPath:can instantiate new one.

-dequeueReusableCellWithIdentifier:总是需要检查,如果返回了一个单元格,同时
-dequeueReusableCellWithIdentifier:forIndexPath:可以实例化新的单元格。

回答by sapi

If you have not defined a prototype cell with the identifier @"cell"in Storyboard, you will get an assertion error when you attempt to dequeue it.

如果你没有@"cell"在 Storyboard 中定义一个带有标识符的原型单元,当你试图将它出列时你会得到一个断言错误。

You can fix this by setting the Identifierproperty on the prototype cell (select the cell and set that attribute in the right hand panel).

您可以通过Identifier在原型单元格上设置属性来解决此问题(选择单元格并在右侧面板中设置该属性)。

回答by Ourang-Zeb Khan

A very silly mistake i had done was

我犯过的一个非常愚蠢的错误是

i didn't put the UITableViewDelegate, UITableViewDataSource after the controller class name like my class code was class TagsViewController: UIViewController

我没有把 UITableViewDelegate, UITableViewDataSource 放在控制器类名之后,就像我的类代码是 class TagsViewController: UIViewController

it should have class TagsViewController: UIViewController , UITableViewDelegate, UITableViewDataSource

它应该有类 TagsViewController: UIViewController , UITableViewDelegate, UITableViewDataSource

May be one of you is facing due to this all other code was ok.

可能是你们中的一个人因此而面临所有其他代码都可以。

回答by user1802778

You need to call "initWithStyle" in custom TableViewCell and initialise the objects again.

您需要在自定义 TableViewCell 中调用“initWithStyle”并再次初始化对象。

Example: ProductTableViewCell.m file

示例:ProductTableViewCell.m 文件

@implementation ProductTableViewCell

- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
   [super setSelected:selected animated:animated];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(70, 0, 320, 60))];
        [self.contentView addSubview:_titleLabel];
   }
   return self;
}

In the main implementation file

在主实现文件中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productTableViewCell"];
    NSDictionary *dic = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        dic = [_filteredArray objectAtIndex:indexPath.row];
    } else {
        dic = [_originalArray objectAtIndex:indexPath.row];
    }
    cell.titleLabel.text = [dic objectForKey: @"title"];
    return cell;
}

回答by Septronic

I had the same error, and I managed to find the fault. I had an array for the segues and view titles:

我有同样的错误,我设法找到了错误。我有一个用于 segue 和视图标题的数组:

NSArray *MMTitles= [NSArray arrayWithObjects:@"MainMenu",@"viewIt",@"viewNots",@"MyProfile",@"Settings",@"Instructions",@"Help", nil];
NSArray *MMSegues=[NSArray arrayWithObjects:@"MainMenu",@"MyProfileSegue",@"viewNotSegue",@"MyProfileSegue",@"SettingsTableViewSegue",@"InstructionsViewSegue",@"HelpViewSegue", nil];

self.menuItems = [[NSArray alloc]initWithObjects:MMTitles,MMSegues, nil];

I then used this array as the datasource for my table. The error I was receiving was due to the fact that I didn't in fact had the HelpViewSeguedeclared in my Storyboard when I instantiated the VC:

然后我使用这个数组作为我的表的数据源。我收到的错误是由于HelpViewSegue我在实例化 VC 时实际上并没有在我的 Storyboard 中声明:

    vc = [mainStoryboard instantiateViewControllerWithIdentifier: [[self.menuItems objectAtIndex:1]objectAtIndex:indexPath.row]];

Pretty trivial, but it was pretty frustrating! Hope this helped.

很琐碎,但很令人沮丧!希望这有帮助。

回答by Chowdary

In the below code you have written @"cell"(written with a small c), but you have to use @"Cell"(Cmust be capital).

在下面的代码中你已经写好了@"cell"(用小写的c),但是你必须使用@"Cell"C必须是大写)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];