ios - cellForRowAtIndexPath 函数异常 - UITableView

标签 ios objective-c uitableview

我想在“UITableView”上显示数据列表。

当我运行代码时,出现以下错误:

*** 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'

当我运行下面的代码时,出现上面给出的错误。错误被抛出 cellForRowAtIndexPath 函数。我该如何解决这个错误?

@interface TheViewController ()

@property (strong, nonatomic) NSMutableArray *myTData;
@property (weak, nonatomic) IBOutlet UITableView *tableViewOutlet;

@end

@implementation TheViewController
- (void)viewDidLoad
{
    self.tableViewOutlet.delegate = self;
    self.tableViewOutlet.dataSource = self;
    self.tableViewOutlet.scrollEnabled = YES;
    self.myData = [[NSMutableArray alloc] initWithObjects:@"obj1", @"obj2", nil];
    [self.tableViewOutlet reloadData];
    [super viewDidLoad];
}

#pragma mark UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return myData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hede"];
    }
    [cell.textLabel setText: [myData objectAtIndex:indexPath.row]];
    [cell.detailTextLabel setText:@"hede hodo"];
    //
// Configure the cell..
    return cell;
}

#pragma mark UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

@end

最佳答案

异常正如它所说的那样。

在 iOS 5 中,您将调用 [tableView dequeueReusableCellWithIdentifier:CellIdentifier],如果它没有返回一个,您将创建一个。新的调用 -dequeueReusableCellWithIdentifier:forIndexPath: 应该注册一个 NIB 或单元类,然后它总是会成功。

您应该调用

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

在您的 -viewDidLoad 方法中。

关于ios - cellForRowAtIndexPath 函数异常 - UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20097923/

相关文章:

ios - Appcelerator Hyperloop CoreGraphics CGBitmapContextCreate 将不会运行

ios - 在多线程中等待返回

ios - UITableViewCell addTarget 中的按钮不起作用

ios - 使用 JSON 的 uitableview 部分出现问题

项目列表数组上的 PHP/Swift JSON 序列化导致零元素

objective-c - OCUnit 测试现有的 iOS 项目。 "ld: file not found"

ios - 自定义标题与组中的单元格重叠

ios - 如何使用 Rest API 写入 Sharepoint 2013 列表

objective-c - 如何在 NSData 中创建 ip 地址和端口以使用

ios - 从另一个类调用时如何修复处理 PanGestureRecognizer 的错误?