OC: self. 與 _

老師的


@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

//數據源數組
@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation ViewController

//一般使用懶加載來獲取數據源
//在OC里面一般是重新實現getter方法
- (NSMutableArray *)dataArray {
    
    //if (self.dataArray == nil)寫法是錯誤的
    if (_dataArray == nil) {
        
        //self.dataArray = [NSMutableArray array];寫法是正確的
        _dataArray = [NSMutableArray array];
        //讀文件
        NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        for (NSDictionary *dict in array) {
            //創建對象
            News *model = [[News alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            //[self.dataArray addObject:model];寫法正確,因為有值了之后不再走if語句
            [_dataArray addObject:model];
        }
        
//        for (News *n in _dataArray) {
//            NSLog(@"%@", n.title);
//        }
        
    }
    
    //return self.dataArray;寫法是錯誤的
    return _dataArray;
    
}








#pragma mark - UITableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    /**
     self.dataArray和_dataArray
     區別:
        self.dataArray會調用getter方法
     
     兩個在大部分情況下是等價的,有時候需要區分
     */
    
    //return _dataArray.count;寫法是錯誤的
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //1.重用標志
    static NSString *cellId = @"cellId";
    
    //2.獲取可重用的cell
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    //3.獲取不到,創建新的cell
    if (nil == cell) {
        cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    //4.顯示數據
    //News *model = _dataArray[indexPath.row];寫法是正確的,因為數組已經初始化了
    News *model = self.dataArray[indexPath.row];
    cell.model = model;
    
    return cell;
}


===========================================================================

我的


@property (nonatomic, strong) NSMutableArray *dataArray;

@end
@implementation ViewController

- (NSMutableArray *) dataArray {
    
//    if (self.dataArray == nil ){
    //錯誤的, 死循環, get 方法,自己調自己
    if (_dataArray == nil ){
        //set方法
        _dataArray = [NSMutableArray array];
//         _dataArray = [NSMutableArray array];
//        沒區別,調用 set 方法。
        NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"plist" ];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];        
        
        for (NSDictionary *dict in array){
            News *model = [[News alloc] init];            
            [model setValuesForKeysWithDictionary:dict];            
            
            [_dataArray addObject: model];
//            [self.dataArray addObject: model];
            //get 方法, 對的, if判定 ,不走了           
        
        }
        
        
    }
    
    
//    return _dataArray;
    return self.dataArray;
    // 調用 set 方法,錯的。雖然不走 if ,但走``````
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;// 會調用 get 方法

//    return _dataArray;        是不可以的, 不會 調用 get方法。
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellId = @"cellId";
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];   
    
    if (nil == cell ){
        cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }

    
    News *model = self.dataArray[indexPath.row];    
    cell.model = model;   
    return cell;

}



最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 我和鄰居家的狗結下了梁子。仇視從上個月的頭幾天開始。它沒有姓名,估且叫它灰灰吧!這人跟人之間有仇很平常,但是這人跟...
    訴與聆聽閱讀 341評論 0 2
  • 光陰沒有對錯 時間不問聚散離愁 離去的終將不會回頭 留下的最終也會揮手 光陰是 沙 任誰...
    零下二度_9e97閱讀 128評論 0 0
  • 兒子和老公發生了點沖突,他們一起去買衣服,說得好好的,一到商場兒子就管不住自己這也要那也買,老公沒有滿足他的額外要...
    Jane_aad1閱讀 201評論 3 3
  • ?原題目: 何炅自曝被發黑照奚弄 粉絲不撫慰反在評論斗圖 何炅曬出截圖 何炅粉絲曬出其照片 搜狐文娛訊 12月1日...
    若安好便是晴天閱讀 334評論 0 0