老師的
@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;
}