-tableView: cellForRowAtIndexPath:方法不执行问题
2014-11-18 16:00
453 查看
今天在学习UItableView 的时候,定义了一个属性
在ViewDidLoad方法方法中用一下方法实例化
然后实现UItableViewDataSource代理方式
运行以后没有任何数据,也没有错误,跟踪调试后发现-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法没有执行,再继续跟踪,发现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2 {
3 return _dataList.count;
4 }
这个方法的返回值是0,即使 _dataList.count是0,为什么是0,原来在定义的时候把 _dataList 属性写成了weak,所以就没有retain,要写成strong。由于- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 的返回值是0,所以就不会调用-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 了。
关于weak和strong看这篇文章http://www.2cto.com/kf/201303/192824.html;
@property (weak, nonatomic) NSMutableArray *dataList;
在ViewDidLoad方法方法中用一下方法实例化
_dataList = [NSMutableArray arrayWithCapacity:20]; for (NSInteger i = 0; i < 20; i++) { Book *book = [[Book alloc]init]; NSString *string = [NSString stringWithFormat:@"iOS进阶(%ld)", i]; [book setBookName:string]; [book setPrice:98.98]; [_dataList addObject:book]; }
然后实现UItableViewDataSource代理方式
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @"bookCell"; BookCell *bookCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (bookCell == nil) { bookCell = [[BookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; NSBundle *bundle = [NSBundle mainBundle]; NSArray *array = [bundle loadNibNamed:@"BookCell" owner:nil options:nil]; bookCell = [array lastObject]; } Book *book = _dataList[indexPath.row]; bookCell.bookPrice.text = book.bookPrice; bookCell.bookName.text = book.bookName; return bookCell; }
运行以后没有任何数据,也没有错误,跟踪调试后发现-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法没有执行,再继续跟踪,发现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2 {
3 return _dataList.count;
4 }
这个方法的返回值是0,即使 _dataList.count是0,为什么是0,原来在定义的时候把 _dataList 属性写成了weak,所以就没有retain,要写成strong。由于- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 的返回值是0,所以就不会调用-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 了。
关于weak和strong看这篇文章http://www.2cto.com/kf/201303/192824.html;
相关文章推荐
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不执行的问题
- 关于UITableView 不能回调 tableView: cellForRowAtIndexPath的问题
- tableView代理方法的调用时间,(主要是heightForRowAtIndexPath和cellForRowAtIndexPath调用时间)
- tableview:cellForRowAtIndexPath:不执行的原因
- 为什么TableView 的代理方法cellForRowAtIndexPath 不执行问题
- 【iOS】tableView:cellForRowAtIndexPath: 方法未调用
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法意思
- tableview 代理方法cellForRowAtIndex中 return cell用法不当导致的问题。
- Ios 该图显示其出现的相关问题定义UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- UITableView不掉用tableView: cellForRowAtIndexPath方法
- Ios 自定义表示图出现的相关问题UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- 使用storyboard显示UITableView时,如果不修改系统默认生成的tableView:cellForRowAtIndexPath:方法中的代码,则必须为UITableViewCell注册(填写)重用标识符:identifier.必须要代码方法中的标识符一致.
- UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath
- ios tableview didSelectRowAtIndexPath方法中,获取某个cell的实例
- CollectionViewCell 不执行 cellForItemAtIndexPath 方法的问
- 关于错误 reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
- UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath
- iOS 开发,UITableViewController中手势关闭cell中的键盘。且不影响tableView中的 didSelectRowAtIndexPath 方法。
- 关于Tableview 方法insertRowsAtIndexPaths 报错问题
- UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath