UITableView中的断言失败configureCellForDisplay:forIndexPath:

Jas*_*lor 48 cocoa-touch objective-c uitableview ios

看过其他类似的问题,我不确定错误在哪里.我收到了断言失败.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
Run Code Online (Sandbox Code Playgroud)

我认为这很简单,但希望有人可以提供帮助.

以下是我的代码:

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

}
Run Code Online (Sandbox Code Playgroud)

Vik*_*ica 107

你永远不会创建一个单元格,你只是尝试重用一个出列的单元格.但是你从未创造过一个,没有.

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

或尝试(仅限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;
}
Run Code Online (Sandbox Code Playgroud)

来自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
Run Code Online (Sandbox Code Playgroud)

-dequeueReusableCellWithIdentifier:如果返回一个单元格,将始终需要检查,同时
-dequeueReusableCellWithIdentifier:forIndexPath:可以实例化新单元格.

  • @sapi不是这样,如果您使用的是故事板原型或者已经注册了一个笔尖或类以供重用,它只会创建一个单元格. (9认同)
  • 他正在做的事情是完全可以的.`dequeueReusableCellWithIdentifier`将创建单元格(如果它不存在). (2认同)

sap*_*api 13

如果尚未@"cell"在Storyboard中定义带有标识符的原型单元格,则在尝试将其出列时会出现断言错误.

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


Our*_*han 7

我做的一个非常愚蠢的错误是

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

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

可能是你们中的一个因为这个所有其他代码都没问题.