cell带xib拖拽
.h拖拽后声明方法
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;
@property (weak, nonatomic) IBOutlet UILabel *label4;
@property (weak, nonatomic) IBOutlet UILabel *label5;
-(void)setValueForCell:(MyModel *)model;
.m 赋值
-(void)setValueForCell:(MyModel *)model{
if (model) {
self.imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.coverLarge]]];
self.label1.text = model.title;
NSString * timeStampString = [NSString stringWithFormat:@"%@",model.createdAt];
NSTimeInterval interval = [timeStampString doubleValue] /1000.0;
NSDate * date = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM"];
self.label2.text = [formatter stringFromDate: date];
self.label3.text = [NSString stringWithFormat:@"%@万",model.duration];
self.label4.text = [NSString stringWithFormat:@"%@��",model.likes];
self.label5.text = [NSString stringWithFormat:@"⏰%@",model.playtimes];
}
}
主界面代码如下搭建界面
#import "ViewController.h"
#import "HeaderView.h"
#import "MyCell.h"
#import "MyModel.h"
#import "AFHTTPSessionManager.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) HeaderView * headerView;
@property(nonatomic,strong) NSMutableArray * dataSource;
@property(nonatomic,strong)NSDictionary * dic ;//表格头视图的数据
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setNav];
[self.view addSubview:self.tableView];
[self requestData];
}
-(void)requestData{
AFHTTPSessionManager * mannage = [AFHTTPSessionManager manager];
mannage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
NSString *urlString = @"http://mobile.ximalaya.com/mobile/v1/album/ts-1531886212572?ac=4G&albumId=14659743&device=iPhone&isAsc=true&pageSize=20&source=0&statEvent=pageview%2Falbum%4012610571&statModule=精品&statPage=tab%40发现_推荐&statPosition=1";
//url中含有中文解决方法
NSString* encodedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//他的这两个回调实在主线程里执行的,所以直接可以做UI操作
[mannage GET:encodedString parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//数据请求成功的回调
self.dic = responseObject[@"data"][@"album"];
self.dataSource = [[NSMutableArray alloc]init];
for (NSDictionary * dic in responseObject[@"data"][@"tracks"][@"list"]) {
MyModel * model = [MyModel new];
[model setValuesForKeysWithDictionary:dic];
[self.dataSource addObject:model];
}
dispatch_async(dispatch_get_main_queue(), ^{
//刷新表格
[self.tableView reloadData];
//表格头视图数据
[self.headerView setValueForHeaderView:self.dic];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//请求失败的操作
NSLog(@"%@",error.description);
}];
}
-(void)setNav{
self.title = @"专辑详情";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"..." style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationController.navigationBar.translucent = NO;
}
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT) style:UITableViewStylePlain];
}
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"cell"];
self.headerView = [[HeaderView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
_tableView.tableHeaderView = self.headerView;
return _tableView;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (self.dataSource.count >0) {
[cell setValueForCell:self.dataSource[indexPath.row]];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 60)];
imageV.image = [UIImage imageNamed:@"1"];
return imageV;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 60;
}
@end