iOS - Objective-C 无法使具有标识符的单元格出队

标签 ios objective-c iphone uitableview menu

'无法使具有标识符新闻的单元格出列 - 必须为标识符注册一个 nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元格'

#import "SidebarViewController.h"
#import "SWRevealViewController.h"



@interface SidebarViewController ()

@property (nonatomic, strong) NSArray *menuItems;
@end



@implementation SidebarViewController


    NSArray *menuItems;



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        //custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    menuItems = @[@"title", @"news ", @"comments", @"map", @"calendar", @"wishlist", @"bookmark", @"tag"];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return menuItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}

@end

标识符看起来像:

enter image description here

我能用它做什么?

我正在尝试添加左侧滑动菜单,但是当我按下菜单按钮时它崩溃了。

最佳答案

您实际上非常接近,您的问题是许多新开发人员感到沮丧的根源。 dequeueReusableCell 有两个不同的、相互排斥的(竞争?)API,您不小心将它们混在一起了。

1.

- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier 

在iOs2.0中引入。
此方法能够返回 nil,当它返回时,由您来创建单元格。

2.

- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath

推出了iOs6.0 此方法不应返回 nil。它要求您为表中的每个索引路径注册一个 UITableViewCell 类和标识符,然后为您“自动”创建单元格。 (这与 UICollectionView 的引入相吻合,这就是它们的工作原理)

解决方案。 您使用的是第一种方法(很好),但您使用的是第二种方法。 将您的 cellForRow... 方法更改为如下内容:

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

    NSString *CellIdentifier = @"someStaticString" ; 
    //btw, it is poor practice to name variables with a capital. Save that for classes/types
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[MYcellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; 
    }
    //configure cell
    return cell;
}

或者... 向 tableView 注册您的单元格类/原型(prototype)和标识符组合(是的,您可以对所有索引使用相同的设置..),然后保持代码不变。

恕我直言,这是一个非常常见的错误,并且没有很好地记录下来,只有从 iOs6 之前就开始开发并引入第二种方法的人才能在没有帮助的情况下看到差异。最好的。

关于iOS - Objective-C 无法使具有标识符的单元格出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390690/

相关文章:

ios - 所有 View Controller 和导航 View Controller 都位于状态栏下方

ios - 自定义选项卡栏以在 View Controller 之间导航

ios - 在 View Controller 之间传递数据

ios - 翻转和旋转

ios - UIPageViewController:页面堆叠在另一个之上

ios - 监听 UITextField setText

ios - 在 Xcode 4.3 上为模拟器构建 OpenSSL 失败

ios - 在 Swift 中设置 UIDatePicker 的文本颜色

iphone - 如何让 Sprite 跟随轨迹?

ios - 如何调整单元格的高度以便内容可以适合它?