查看其他类似问题后,我不确定错误出在哪里。 我收到了一个断言失败。
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
我认为这很简单,但希望有人能提供帮助。
下面是我的代码:
#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;
}
最佳答案
您永远不会创建单元格,您只是尝试重用已出列的单元格。但由于您从未创建过,所以没有。
- (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;
}
来自 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: 总是需要检查,如果一个单元格被返回,而
-dequeueReusableCellWithIdentifier:forIndexPath: 可以实例化一个新的。
关于ios - UITableView configureCellForDisplay :forIndexPath: 中的断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15311201/