ios - 无法从其 DataSource 获取单元格

下面的代码有什么问题

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

    NSString *CellIdentifier  = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell.textLabel.text = @"hello";

    return cell;
}

还有

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

但我无法从其数据源获取单元格

整个异常是

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7ffe0b092800; frame = (0 97; 414 590); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7ffe0acf3b10>; layer = <CALayer: 0x7ffe0aceb6e0>; contentOffset: {0, 0}; contentSize: {414, 88}>) failed to obtain a cell from its dataSource (<AllContactsViewController: 0x7ffe0ace60f0>)'

我已经为表格 View 设置了委托(delegate)和数据源

最佳答案

使用 Swift 3 我忘记将 UITableViewDataSource 和 UITableViewDelegate 协议(protocol)添加到 ViewController 声明时遇到此错误。

类 DataViewController:UIViewController、UITableViewDataSourceUITableViewDelegate

添加它们可以解决问题。

https://stackoverflow.com/questions/34250595/

相关文章:

ios - 检测 iPhone 应用程序中是否存在摄像头?

ios - 如何在运行时更改约束优先级

ios - 如何检查 NSArray 中的对象是否为 NSNull?

ios - 永久设置 UITableView 内容插入

ios - 为什么 UIViewController 在 UINavigationBar 下扩展,而

ios - UITableViewCell 选中行的文字颜色变化

iphone - UISegmentedControl 文本以编程方式

iphone - 快速将单个引脚添加到 MKMapView?

ios - 禁用 UITextField 的自动更正

objective-c - 请求访问相机胶卷的权限