//
// ViewController.m
// UI08_作业
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "ViewController.h"
#import "CarNews.h"
#import "CarNewsCell.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) NSMutableArray *carNewsArray;
@end
@implementation ViewController
- (void)getDate {
self.carNewsArray = [NSMutableArray array];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSDictionary *dataDic = dic[@"data"];
NSArray *listArray = dataDic[@"list"];
for (NSDictionary *itemDic in listArray) {
CarNews *carNews = [[CarNews alloc] init];
[carNews setValuesForKeysWithDictionary:itemDic];
[self.carNewsArray addObject:carNews];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self getDate];
NSLog(@"%@",self.carNewsArray);
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
tableView.rowHeight = 100;
[self.view addSubview:tableView];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 200)];
headerView.backgroundColor = [UIColor cyanColor];
tableView.tableHeaderView = headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.carNewsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CarNews *carNews = self.carNewsArray[indexPath.row];
static NSString *carNewsId = @"carNewsCell";
CarNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:carNewsId];
if (nil == cell) {
cell = [[CarNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:carNewsId];
}
cell.carNews = carNews;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// CarNewsCell.h
// UI08_作业
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CarNews.h"
@interface CarNewsCell : UITableViewCell
@property (nonatomic,retain) CarNews *carNews;
@end
//
// CarNewsCell.m
// UI08_作业
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "CarNewsCell.h"
#import "UIImageView+WebCache.h"
@interface CarNewsCell ()
@property (nonatomic,retain) UIImageView *carNewsImageView;
@property (nonatomic,retain) UILabel *carNewsTitleLabel;
@end
@implementation CarNewsCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.carNewsImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.carNewsImageView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.carNewsImageView];
self.carNewsTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.carNewsTitleLabel.backgroundColor = [UIColor greenColor];
[self.contentView addSubview:self.carNewsTitleLabel];
}
return self;
}
-(void)layoutSubviews {
[super layoutSubviews];
self.carNewsImageView.frame = CGRectMake(10, 10, 120, 80);
self.carNewsTitleLabel.frame = CGRectMake(self.carNewsImageView.frame.origin.x + self.carNewsImageView.frame.size.width + 10, self.carNewsImageView.frame.origin.y, 150, 40);
}
//赋值
- (void)setCarNews:(CarNews *)carNews {
if (_carNews != carNews) {
_carNews = carNews;
self.carNewsTitleLabel.text = carNews.title;
//参数1:网络地址
//参数2:占位图
[self.carNewsImageView sd_setImageWithURL:[NSURL URLWithString:carNews.picCover] placeholderImage:nil];
}
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
//
// CarNews.h
// UI08_作业
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CarNews : NSObject
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *picCover;
@end
//
// CarNews.m
// UI08_作业
//
// Created by lanou3g on 17/8/15.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "CarNews.h"
@implementation CarNews
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end