本文介绍了UITableView中的断言失败configureCellForDisplay:forIndexPath:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这里的错误在哪里,看过其他类似的问题。
我收到一个断言失败。

 由于未捕获异常NSInternalInconsistencyException终止应用程序,原因:UITableView dataSource必须返回来自tableView的单元格:cellForRowAtIndexPath:

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

以下是我的代码:

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

}


解决方案

从不创建一个单元格,你只是试图重用一个出列单元格。

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

或尝试(仅限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;
}






   - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; //由委托使用以获取已分配的单元,而不是分配新的单元。 
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue方法保证单元格被正确返回和调整,假设标识符已注册

<$ c如果返回了单元格,则$ c> -dequeueReusableCellWithIdentifier:将始终需要检查,

-dequeueReusableCellWithIdentifier:forIndexPath:可以实例化新的。


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;

}
解决方案

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+)

- (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

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

这篇关于UITableView中的断言失败configureCellForDisplay:forIndexPath:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 05:56