MVC的步骤 以及重点
Contriller.m
#import "MusickPlayTableController.h"
#import "MusickListCell.h"
#import "MusickModel.h"
@interface MusickPlayTableController ()
@property(nonatomic,retain)NSArray*dataArray;
@property(nonatomic,retain) NSMutableArray *musicArray;
@end
@implementation MusickPlayTableController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"听风";
NSURL*url = [NSURL URLWithString:@"http:project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist"];
NSURLRequest*ruquest = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:5.0];
//网络请求
[NSURLConnection sendAsynchronousRequest:ruquest queue:[[NSOperationQueue alloc ]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"错误 %@",connectionError);
return ;
}
//data 是一个plisst 数据,对data进行 反序列化解写
_dataArray=[NSPropertyListSerialization propertyListWithData:data options:0 format:NULL error:NULL];
_musicArray = [NSMutableArray array];
for (NSDictionary * dic in _dataArray) {
NSLog(@"%@",dic);
MusickModel*model=[[MusickModel alloc] init];
[model setValuesForKeysWithDictionary:dic]; //必须写 否则赋不上值
[_musicArray addObject:model]; //必须写 否则赋不上值
// NSLog(@"%@",model.singer);
}
[self.tableView reloadData];
//从新加载
// [self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
// //.....
});
}];
}
///
View.m
#import "MusickListCell.h"
@implementation MusickListCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self =[ super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//歌曲名
CGRect rect1=CGRectMake(180, 20, 200, 40);
_SongNameLabel=[[UILabel alloc] initWithFrame:rect1];
_SongNameLabel.backgroundColor=[UIColor redColor];
[self addSubview:_SongNameLabel];
//歌者名
CGRect rect2=CGRectMake(180, 70, 200, 40);
_SingerNameLabel=[[UILabel alloc] initWithFrame:rect2];
_SingerNameLabel.backgroundColor=[UIColor greenColor];
[self addSubview: _SingerNameLabel];
//歌手头像
CGRect rect3=CGRectMake(0, 0, 100, 200);
_SingerPhtoto=[[UIImageView alloc] initWithFrame:rect3];
_SingerPhtoto.backgroundColor=[UIColor yellowColor];
[self addSubview:_SingerPhtoto];
}
return self;
}
#pragma 数据模型赋值方法
-(void)setMusickMoudel:(MusickModel *)musickMoudel
{
if (_musickMoudel!=musickMoudel) {
// _musickMoudel=[musickMoudel retain];
_SingerNameLabel.text=musickMoudel.artists_name;
_SongNameLabel.text=musickMoudel.name;
_SingerPhtoto.image =musickMoudel.picUrl;
}
}
- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
/
mosel.m
#import "MusickModel.h"
@implementation MusickModel
-(void)setValue:(id)value forKey:(NSString *)key
{
//父类方法 必须写 否则赋不上值
[super setValue:value forKey:key];
}
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
//防止蹦亏
}
@end