zoukankan      html  css  js  c++  java
  • -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;

  • 相关阅读:
    TinySpider开源喽
    TinyXmlParser开源喽
    Tiny快速入门之控制层开发
    TinyDBRouter
    TinyIOC
    开源前要做好哪些准备工作?
    分布式锁的简单实现
    TinyDBCluster Vs routing4db
    HTML5基础(五)
    HTML5基础(四)
  • 原文地址:https://www.cnblogs.com/shanyimin/p/4106019.html
Copyright © 2011-2022 走看看