IVTMyLiveViewController.m 5.61 KB
//
//  IVTMyLiveViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/23.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTMyLiveViewController.h"
#import "IVTMyLiveCollectionViewCell.h"
#import "IVTKSYDianBoViewController.h"
static NSString *cellIdentifier = @"UICollectionViewCell";

@interface IVTMyLiveViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSArray *myLiveListArray;
    UIImageView *myLiveImg;
    UILabel *myLiveLabel;
    
    UICollectionView *myCollectionView;
}
@end

@implementation IVTMyLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigationBar];
    
    [self initUI];
    
    myLiveListArray = [IVTAccountTool getMyLiveListArray];
    if (myLiveListArray.count == 0) {
        [HUDUtil showLoading];
        [IVTAccountTool getMyLiveListRequestSuccess:^(NSArray *myLiveList) {
            [HUDUtil hide];
            myLiveListArray = myLiveList;
            [myCollectionView reloadData];
        } error:^(NSDictionary *successData) {
            NSString *msg = successData[@"description"];
            [HUDUtil showError:msg];
        } failure:^(NSError *failureError) {
            [HUDUtil showError:@"网络异常"];
        }];
    }
}

- (void)setNavigationBar{
    self.navigationItem.title = @"我的直播";
}

- (void)initUI{
    __unsafe_unretained typeof (self) wself = self;
    myLiveImg = [[UIImageView alloc] init];
    myLiveImg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"我的直播--背景条"]];
    [self.view addSubview:myLiveImg];
    [myLiveImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wself.view).offset(5);
        make.left.equalTo(wself.view).offset(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 40));
    }];
    
    myLiveLabel = [[UILabel alloc] init];
    myLiveLabel.text = @"我的直播";
    myLiveLabel.font = [UIFont YXFontOfSize:14];
    myLiveLabel.textAlignment = NSTextAlignmentCenter;
    myLiveLabel.textColor = [UIColor YXColorWithHexCode:@"#5A5A5A"];
    [myLiveImg addSubview:myLiveLabel];
    [myLiveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(myLiveImg).offset(5);
        make.left.equalTo(myLiveImg).offset(0);
        make.size.mas_equalTo(CGSizeMake(85, 30));
    }];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.minimumLineSpacing = 3;
    flowLayout.minimumInteritemSpacing = 1;
    
    myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
    myCollectionView.backgroundColor = [UIColor clearColor];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [myCollectionView registerClass:[IVTMyLiveCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    [self.view addSubview:myCollectionView];
    [myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(myLiveImg.mas_bottom).offset(5);
        make.left.equalTo(wself.view.mas_left);
        make.right.equalTo(wself.view.mas_right);
        make.bottom.equalTo(wself.view.mas_bottom);
    }];
}

#pragma --mark UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return myLiveListArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    IVTMyLiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    NSString *urlString = [[myLiveListArray objectAtIndex:indexPath.row] valueForKey:@"thumbnail"];
    cell.userHeaderImageUrl = urlString;
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section<[myLiveListArray count]) {
        IVTKSYDianBoViewController *dianbo = [[IVTKSYDianBoViewController alloc]init];
        dianbo.userId = [IVTAccountTool getUserId];
        dianbo.roomInfo = myLiveListArray[indexPath.section];
        [self.navigationController pushViewController:dianbo animated:YES];
    }
//    NSLog(@"当前点击的是%ld",indexPath.row);
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma mark --UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = (kScreenWidth - 24)/3;
    return CGSizeMake(itemWidth, itemWidth);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 6, 0, 6);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end