美文网首页
Masonary三方布局音乐列表练习

Masonary三方布局音乐列表练习

作者: 我不白先生 | 来源:发表于2020-11-05 16:40 被阅读0次

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[MusicTableViewController alloc]init]];
    [self.window makeKeyAndVisible];
    return YES;
}

Music.h

@interface Music : NSObject
//声明不可变数组或不可变字符串时,应该是使用copy还是strong
//歌曲名称
@property(nonatomic,strong)NSString *name;
//专辑名
@property(nonatomic,strong)NSString *album;
//歌手
@property(nonatomic,strong)NSString *artist;
//持续时间
@property(nonatomic,assign)NSTimeInterval duration;
//是否高清
//给getter方法 添加一个新的名字
@property(nonatomic,assign,getter=isHighQuality)BOOL highQuality;
//是否下载
@property(nonatomic,assign,getter=isDownloaded)BOOL downloaded;
+(NSArray*)allMusics;
@end

Music.m

#import "Music.h"
@implementation Music
+(NSArray*)allMusics{
    Music *music1 = [[Music alloc]init];
    music1.name = @"塞北的雪";
    music1.album = @"塞北的雪";
    music1.artist = @"齐秦";
    music1.duration = [Music durationWithMinutes:3 andSeconds:19];
    music1.highQuality = YES;
    music1.downloaded = YES;
    
    Music *music2 = [[Music alloc]init];
    music2.name = @"活着";
    music2.album = @"国内金曲";
    music2.artist = @"齐秦";
    music2.duration = [Music durationWithMinutes:4 andSeconds:29];
    music2.highQuality = NO;
    music2.downloaded = YES;
    
    Music *music3 = [[Music alloc]init];
    music3.name = @"无地自容";
    music3.album = @"国内金曲";
    music3.artist = @"黑豹";
    music3.duration = [Music durationWithMinutes:4 andSeconds:29];
    music3.highQuality = YES;
    music3.downloaded = NO;
    
    Music *music4 = [[Music alloc]init];
    music4.name = @"梦回唐朝";
    music4.album = @"国内金曲";
    music4.artist = @"唐朝乐队";
    music4.duration = [Music durationWithMinutes:4 andSeconds:29];
    music4.highQuality = NO;
    music4.downloaded = NO;
    return  @[music1,music2,music3,music4];
}
+(NSTimeInterval)durationWithMinutes:(int)minutes andSeconds:(int)seconds{
    return minutes * 60 + seconds;
}
@end

MusicTableViewController.m

#import "MusicTableViewController.h"
#import "Music.h"
#import "MusicCell.h"
@interface MusicTableViewController ()
@property(nonatomic,strong)NSArray *allMusic;
@end

@implementation MusicTableViewController
-(NSArray *)allMusic{
    if(!_allMusic){
        _allMusic = [Music allMusics];
    }
    return _allMusic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.rowHeight = 70;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allMusic.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //如果要用故事板只能用复用方式二,这里复用方式一方式二都可
    MusicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"music"];
    if(!cell){
        cell = [[MusicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"music"];
    }
    Music *music = self.allMusic[indexPath.row];
    cell.music = music;
    return cell;
}

MusicCell.h

@interface MusicCell : UITableViewCell
@property(nonatomic,strong)Music *music;
@end

MusicCell.m

#import "MusicCell.h"
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"

@interface MusicCell()
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *durationLabel;
@property(nonatomic,strong)UILabel *artistLabel;
@property(nonatomic,strong)UIImageView *downloadImageView;
@property(nonatomic,strong)UIImageView *hqImageView;
@end
@implementation MusicCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self nameLabel];
        [self artistLabel];
        
    }
    return self;
}
-(void)setMusic:(Music *)music{
    self.nameLabel.text = music.name;
    self.artistLabel.text = [[music.artist stringByAppendingString:@"-"]stringByAppendingString:music.album];
    self.durationLabel.text = [NSString stringWithFormat:@"%d:%02d",(int)music.duration / 60,(int)music.duration%60];
    [self.hqImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(0);
    }];
    [self.downloadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(0);
    }];
    if(music.isHighQuality){
        [self.hqImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(20);
        }];
    }
    if(music.isDownloaded){
        [self.downloadImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(20);
        }];
    }
   
}
-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(10);
            make.left.equalTo(20);
            make.height.equalTo(25);
            make.right.equalTo(self.durationLabel.mas_left);
        }];
    }
    return _nameLabel;
}
- (UILabel *)durationLabel{
    if(!_durationLabel){
        _durationLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_durationLabel];
        [_durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(40);
            make.right.equalTo(-8);
            make.centerY.equalTo(0);
        }];
    }
         return _durationLabel;
}
-(UILabel *)artistLabel{
    if(!_artistLabel){
        _artistLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_artistLabel];
        [_artistLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(25);
            make.bottom.equalTo(-8);
            make.right.equalTo(self.durationLabel.mas_left);
            make.left.equalTo(self.hqImageView.mas_right);
        }];
    }
    return _artistLabel;
}
-(UIImageView *)hqImageView{
    if(!_hqImageView){
        _hqImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bdt"]];
        [self.contentView addSubview:_hqImageView];
        [_hqImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(20);
            make.centerY.equalTo(self.artistLabel);
            make.left.equalTo(self.downloadImageView.mas_right);
        }];
    }
    return _hqImageView;
}
-(UIImageView *)downloadImageView{
    if(!_downloadImageView){
        _downloadImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"b3v"]];
        [self.contentView addSubview:_downloadImageView];
        [_downloadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.equalTo(20);
            make.centerY.equalTo(self.artistLabel);
            make.left.equalTo(20);
        }];
    }
    return _downloadImageView;
}

相关文章

网友评论

      本文标题:Masonary三方布局音乐列表练习

      本文链接:https://www.haomeiwen.com/subject/cncavktx.html