您的位置:首页 > 其它

WXHL 学习总结(七)

2015-08-27 08:47 267 查看
               通过一个多月的学习,UI基础已经讲完,能做出一些简单的小案例,从这周开始,就着手开始真正的第一个小项目了。还是有一定的收获的
下面是一些简单的表视图的应用案例:

1 微博 :练习使用自定义单元格,主要是计算好每个单元格的高度,以及单元格内每个控件的frame,因为有些是会员,有的有插图,有的没插图,所以计算略显复杂

#pragma mark - 复写get方法

-(NSArray *)weibos{

    if (_weibos == nil) {

        //获取plist文件路径

        NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];

        //将数据放进数组

        self.weibos = [NSArray arrayWithContentsOfFile:path];

        //定义一个可变数组  存放模型

        NSMutableArray *models = [NSMutableArray array];

        //字典转模型

        for (NSDictionary *dict in self.weibos)
{

            WXWeibo *model = [WXWeibo weiboWithDict:dict];

            [models addObject:model];

            

        }

        

        _weibos = models;

    }

    return _weibos;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.tableView.frame = CGRectMake(0, 64, 375, 603);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark - Table view 数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.weibos.count;

}

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

    

    // 1 获取模型对象

    WXWeibo *weibo = self.weibos[indexPath.row];

    

    // 2 创建单元格

    static NSString *identifier = @"weibo_cell";

    WXWeiboCell *cell = [[WXWeiboCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    

    // 3 给单元格赋值

        cell.weibo = weibo;

    

    // 4 返回单元格

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    WXWeibo *weibo = self.weibos[indexPath.row];

    

    NSString *text = weibo.text;

    

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(350, 1000)];

    

    

    //如果有图片  加上图片的高度

    if (weibo.picture) {

        return size.height + 40 + 35 + 100;

    }else{

        //没有图片  返回正文高度 头像高度  之和

        return size.height + 30 +35;

    }

}

效果图: 


2.  汽车列表、LOL英雄列表   :  练习单元格的使用,以及重用问题

汽车展示:分组展示每个系列的车   右侧通过方法添加索引

英雄展示:可以点击单元格,修改英雄的名字

效果图:









 


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: