码迷,mamicode.com
首页 > 其他好文 > 详细

tableview的问题小结

时间:2016-10-15 02:16:50      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

问题1:什么是懒加载?什么是字典转模型?模型又是什么?为什么要写懒加载?self.属性和_属性的使用注意?为什么控件要用weak? string要用copy?

懒加载也成为延迟加载,只有在需要加载的时候才去加载,其实就是重写属性的getter方法

 

#import "CWViewController.h"
#import "CWCarModel.h"
//设置数据源方法
@interface CWViewController ()<UITableViewDataSource>
@property (nonatomic,weak)UITableView *tableView;
@property (nonatomic,strong)NSArray *dataArray;
@end

@implementation CWViewController
//懒加载
- (NSArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"cars" ofType:@"plist"]];
        //字典转模型
        //1.创建可变数组
        NSMutableArray *nmArray = [NSMutableArray array];
        //2.遍历字典数组
        for (NSDictionary *dict in _dataArray) {
            //字典转模型
            CWCarModel *model = [CWCarModel carsWithDict:dict];
            //将模型添加到可变数组
            [nmArray addObject:model];
            
        }
        //将可变数组中的模型赋值给字典数组
        _dataArray = nmArray;
        
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.dataArray;
    
    [self setUpUI];
}
- (void)setUpUI {
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
#pragma mark -- 以上是做了数据处理,把要访问的数组存储到了字典数组中,下面是为了把数据显示出来
    //设置代理对象
    self.tableView.dataSource = self;
    
}
#pragma mark -- 实现协议方法
//返回多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
//每组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //获取模型对象
    CWCarModel *model = self.dataArray[section];
    return model.cars.count;
}
//每行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //实例化控件
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    //1.获取模型对象
    CWCarModel *model = self.dataArray[indexPath.section];
    //2.获取汽车名
    NSString *str = model.cars[indexPath.row];
    //3.设置文字信息
    cell.textLabel.text = str;
    return cell;
}
#pragma mark -- 返回头尾组标题
//组头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    CWCarModel *model = self.dataArray[section];
    return model.title;
}
//组尾
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    CWCarModel *model = self.dataArray[section];
    return model.desc;
}
@end

 

tableview的问题小结

标签:

原文地址:http://www.cnblogs.com/yiyuanchenfeng/p/5962500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!