UITableView reloadData之后的NSInternalInconsistencyException

时间:2015-01-25 11:21:04

标签: ios objective-c uitableview

我在NSMutableArray之后添加UITableView中与我reloadData关联的对象时遇到问题...

我收到了这个错误:

*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:7344
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

添加:

self.relatedArtists = [[NSMutableArray alloc]init];

Artist* test = [[Artist alloc]init];
test.name = @"Test 1";
[self.relatedArtists addObject:test];

Artist* test2 = [[Artist alloc]init];
test2.name = @"Test 2";
[self.relatedArtists addObject:test2];

[self.artists reloadData];

numberOfRowsInSection& cellForRowAtIndexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.relatedArtists count];
}

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

    UITableViewCell *cell = [self.artists dequeueReusableCellWithIdentifier:@"ARTIST_CELL_ID"];

    Artist *artist = self.relatedArtists[indexPath.row];
    cell.textLabel.text = artist.name;

    return cell;
}

我想我错过了Main.storyboard档案中的内容,但我不知道是什么......

2 个答案:

答案 0 :(得分:1)

我认为您错过了将故事板中tableView单元格的标识符设置为ARTIST_CELL_ID

答案 1 :(得分:0)

您还必须使用给定的reusablecellidentifier“ARTIST_CELL_ID”注册tableviewcell。

示例代码如下。

[yourTableView registerNib:[UINib nibWithNibName:@"YouTableViewCellNibName" bundle:nil] forCellReuseIdentifier:@"CellIdentifier"];