当前位置:   iOS > 正文

无法从其DataSource获取单元格

objective-c,uitableview,ios,swift,xcode,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

以下代码有什么问题

-(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;
}

但我无法从其dataSource获取一个单元格

整个例外是

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (; layer = ; contentOffset: {0, 0}; contentSize: {414, 88}>) failed to obtain a cell from its dataSource ()'

我为表视图设置了委托和数据源



1> 小智..:

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

class DataViewController:UIViewController,UITableViewDataSource,UITableViewDelegate

添加它们解决了问题.



2> TwoStraws..:

您的错误表明由于某种原因cellForRowAtIndexPath返回nil,我猜这是因为您未能将可重复使用的单元格出列.如果你想确认这一点,只需在当前的出列呼叫后设置一个断点:我希望你能找到cell设置为nil.

如果您正在使用现代Xcode模板,您可以获得为您制作的原型单元,那么您应该使用它:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

如果您没有使用Xcode模板,请使用该行代码,然后注册您自己的重用标识符,如下所示:

[self.tableView registerClass:[UITableViewCell self] forCellReuseIdentifier:@"Cell"];

一切都很好,应该解决问题.我为Swift用户更详细地写了这篇文章.



3> Vivek..:
Swift 3.0

您只需要添加UITableViewDelegateUITableViewDataSource

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

下图仅供参考.

在此输入图像描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/iOS/detail/8191
推荐阅读
相关标签
  

闽ICP备14008679号