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

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

                        

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代理方式atom

 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 方法没有执行,再继续跟踪,发现spa

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

相关文章
相关标签/搜索