-tableView: cellForRowAtIndexPath:方法不执行问题

今天在学习UItableView 的时候,定义了一个属性

1 @property (weak, nonatomic) NSMutableArray *dataList;

在ViewDidLoad方法方法中用一下方法实例化

 1  _dataList = [NSMutableArray arrayWithCapacity:20];
 2
 3     for (NSInteger i = 0; i < 20; i++)
 4
 5 {
 6
 7       Book *book = [[Book alloc]init];
 8
 9       NSString *string = [NSString stringWithFormat:@"iOS进阶(%ld)", i];
10
11      [book setBookName:string];
12
13      [book setPrice:98.98];
14
15      [_dataList addObject:book];
16
17 }

然后实现UItableViewDataSource代理方式

 1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 2 {
 3         return _dataList.count;
 4 }
 5
 6 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 7 {
 8    static NSString * CellIdentifier = @"bookCell";
 9    BookCell *bookCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
10    if (bookCell == nil)
11   {
12       bookCell = [[BookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
13       NSBundle *bundle = [NSBundle mainBundle];
14       NSArray *array = [bundle loadNibNamed:@"BookCell" owner:nil options:nil];
15       bookCell = [array lastObject];
16   }
17
18   Book *book = _dataList[indexPath.row];
19   bookCell.bookPrice.text = book.bookPrice;
20   bookCell.bookName.text = book.bookName;
21
22   return bookCell;
23
24 }

运行以后没有任何数据,也没有错误,跟踪调试后发现-(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;

时间: 2024-08-15 23:06:47

-tableView: cellForRowAtIndexPath:方法不执行问题的相关文章

cellForRowAtIndexPath方法不执行的那些坑

今天用到了uitableview,是xib形式的.不过cellForRowAtIndexPath方法死活不执行,检查了返回的row数量,section的数量,数据源,代理都没问题,不过cellForRowAtIndexPath还是死活不执行,查看视图发现view中的tableview也还是看不到.几乎要崩溃.... 最后发现是约束有问题,我设置了tableview的约束是和它的父视图同等高度.我猜这可能和tableview的加载过程有关吧,检测到tableview的高度为0,直接就不向数据源对象

tableView didSelectRowAtIndexPath方法不执行

最近做了一个登陆的页面,该页面主要有输入用户名,密码 为了方便用户有多个账号登陆,其实--也是为了方便我们开发人要,可以避免每次输入一大串手机号. 对于每次登陆成功后,将手机号保存到NSUserDefault中,注意它支持的类型有:NSNumber(NSInteger.float.double),NSString,NSDate,NSArray,NSDictionary,BOOL. -(void)saveUserMobile:(NSString *)mobile { NSUserDefaults

tableView创建方法调用的研究

当两个section的cell数量都为5的时候,方法的调用顺序: -[ViewController numberOfSectionsInTableView:] -[ViewController tableView:titleForHeaderInSection:]-[ViewController tableView:titleForFooterInSection:]-[ViewController tableView:numberOfRowsInSection:], section = 1-[V

tableView代理方法的调用时间,(主要是heightForRowAtIndexPath和cellForRowAtIndexPath调用时间)

最近做一个demo,涉及按照数据分类然后依照分类在 cellForRowAtIndexPath形成不同类型的cell,就是有判断(在viewdidload里面做)和形成(在 cellForRowAtIndexPath做)两个阶段.这个时候我就有了一个疑问,viewdidload和 cellForRowAtIndexPath是什么顺序进行的,于是在几个函数里面记录打印了一下过程: 2015-03-03 13:14:20.970 whenToCom[9790:1381585] -[Contacter

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不执行的问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不执行基本上只有3个原因 1. delegate没设,一定要有self.tableView.delegate = self 2. section,row的个数返回为0,或者有冲突 3. tableView的frame有问题,比如位(0,0,0,0) 我遇到的问题比较奇葩,因为我喜欢在代码里添加约

ios tableview didSelectRowAtIndexPath方法中,获取某个cell的实例

选中tableView的某一行,触发如下方法: -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } 若此时需要对tableview的cell做处理,就需要先得到改行cell对应的实例,可运用如下方法: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 或者某个自定义cell

tableview 代理方法详解

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) { UITableViewCellAccessoryNone, // 不显示任何图标 UITableViewCellAccessoryDisclosureIndicator, // 跳转指示图标 UITableViewCellAccessoryDetailDisclosureButton, // 内容详情图标和跳转指示图标 UITableViewCellAccessoryCheckm

iOS 一个ViewController上显示2个tableView的方法

1.在StoryBoard上创建2个tableView,并用autolayout约束. 2.在ViewController上拖进来. @property (weak, nonatomic) IBOutlet UITableView *leftTableView; @property (weak, nonatomic) IBOutlet UITableView *rightTableView; 3.实现代理方法: 重点:区分tableView的方法就是用对象比对的方法,传进来的tableView是

Ios 该图显示其出现的相关问题定义UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:&amp;#39;

解决这个问题 在 加上个 标示符 Cell 自己定义 customCell .h 代码例如以下 ViewController.m 文件里 代码例如以下 执行结果 吕 图坚持直接在这里 不行 Ios 该图显示其出现的相关问题定义UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'