ios – 无法使用标识符进行单元格排队Cell – 必须为标识符注册一个nib或类,或者连接一个故事板中的原型单元格?

我试图在用户的库中显示歌曲列表的TableView.我使用了 this tutorial的代码(它使用了一个故事板,但是我想尝试一种不同的方式,只有一个UITableView的子类).

我得到错误:

*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:],/SourceCache/UIKit/UIKit-2903.23/UITableView.m:5261
2014-05-07 20:28:55.722 Music App[9629:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

和一个错误线程1:SIGABRT在线:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

这是我的代码:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
    NSArray *songs = [songsQuery items];

    return [songs count];
}


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

    // Configure the cell...

    MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
    NSArray *songs = [songsQuery items];

    MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row];

    cell.textLabel.text = [rowItem valueForProperty:MPMediaItemPropertyTitle];
    cell.detailTextLabel.text = [rowItem valueForProperty:MPMediaItemPropertyArtist];

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Table-view-background.png"]];
    cell.textLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1.0];

    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Table-view-selected-background.png"]];

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;
}

该应用程序加载和工作正常,显示一个空白的表,当我在我的Mac上的iPhone模拟器中运行它.当我在iPhone上运行时会出现这个错误.

任何帮助将不胜感激,谢谢!

解决方法

如果您以编程方式创建表视图,并且您只是使用默认的UITableViewCell,那么您应该注册该类(在viewDidLoad中是一个好的地方).您还可以为自定义类执行此操作,但只有在代码中创建单元格(及其子视图)(使用registerNib:forCellWithReuseIdentifier:如果单元格是在xib文件中创建的).
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

但是,这只会给你一个没有detailTextLabel的“基本”表视图单元格.要获取这种类型的单元格,您应该使用较短的出队方法dequeueReusableCellWithIdentifier:,如果没有找到具有该标识符的单元格,则不会抛出异常,然后使用if(cell == nil)子句创建单元格,

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    //Configure cell
   return cell;
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...