IVTMeMiTangViewController.m 7.94 KB
//
//  IVTMeMiTangViewController.m
//  IVTMeLive
//
//  Created by XRG on 16/5/31.
//  Copyright © 2016年 Joky. All rights reserved.
//

#import "IVTMeMiTangViewController.h"
#import "Masonry.h"
#import "UIColor+IVTMeColor.h"
#import "UIFont+IVTMeFont.h"
#import "IVTMeSupportFirstCell.h"
#import "IVTMeSupportCommonCell.h"
#import "IVTUserInfoCenterViewController.h"

@interface IVTMeMiTangViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *imageArray;
    NSMutableArray *requestDataArray;
    UILabel * noDataLabel;
}
@property (nonatomic, retain) UITableView *hintTable;

@end

@implementation IVTMeMiTangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    requestDataArray = [[NSMutableArray alloc]initWithCapacity:10];

    self.view.backgroundColor = [UIColor IVTBackgroundColor];

    [self buildHintTable];
    imageArray = @[@"贡献榜第一名",@"贡献榜第二名",@"贡献榜第三名"];
    [self request];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doChangeFollowStatus) name:@"followedChanged" object:nil];
    noDataLabel = [[UILabel alloc]init];
    noDataLabel.font = [UIFont systemFontOfSize:13];
    noDataLabel.frame = CGRectMake(kScreenWidth / 2 - 100, kScreenWidth / 2 + 50, 200, 100);
    noDataLabel.text = @"没有数据";
    noDataLabel.hidden = YES;
    noDataLabel.font = [UIFont systemFontOfSize:16];
    noDataLabel.text = @"空空如也~~";
    noDataLabel.textAlignment = 1;
    [self.view addSubview:noDataLabel];
}

- (void)doChangeFollowStatus{
    [_hintTable reloadData];
}

-(void)initNav{
    
    //整体背景nav view
    UIView* navBgView = [[UIView alloc] init];
    [self.view addSubview:navBgView];
    navBgView.backgroundColor  = [UIColor IVTLightRedColor];
    __unsafe_unretained typeof (self) wself = self;
    [navBgView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(wself.view.mas_left);
        make.right.equalTo(wself.view.mas_right);
        make.top.equalTo(@0);
        make.height.equalTo(@58);
    }];
    
    UILabel *setting = [[UILabel alloc]init];
    [navBgView addSubview:setting];
    setting.text = @"蜜糖贡献榜";
    setting.textColor = [UIColor YXColorWithHexCode:@"#FFFFFF"];
    setting.font = [UIFont YXFontOfSize:19];
    [setting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navBgView.mas_centerX);
        make.top.equalTo(wself.view.mas_top).with.offset(29);
        make.size.mas_equalTo(CGSizeMake(100.0f, 19.0f));
    }];
    
    //背景nav 返回上级页面按钮
    UIButton* backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [navBgView addSubview:backButton];
    UIImage* navImage = [UIImage imageNamed:@"返回键"];
    [backButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(wself.view);
        make.top.equalTo(wself.view.mas_top).with.offset(18);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        
    }];
    [backButton setImage:navImage forState:UIControlStateNormal];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(5,5,5,5);
    [backButton addTarget:self action:@selector(backToTopPage:) forControlEvents:UIControlEventTouchUpInside];
}

//返回按钮点击
-(void)backToTopPage:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)buildHintTable
{
    if (_hintTable==nil) {
        self.hintTable = [[UITableView alloc] init];
        _hintTable.backgroundColor = [UIColor IVTBackgroundColor];
        _hintTable.frame = CGRectMake(0, 58, kScreenWidth   , kScreenHeight-58);
        _hintTable.delegate = self;
        _hintTable.dataSource = self;
        _hintTable.showsHorizontalScrollIndicator = NO;
        _hintTable.showsVerticalScrollIndicator = NO;
        _hintTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _hintTable.tableFooterView = [UIView new];
        [self.view addSubview:_hintTable];
    }
}

#pragma Mark - 网络请求
-(void)request{
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    if (_isMain==YES) {
         [dict setObject:[IVTAccountTool getUserId] forKey:@"userId"];
    }else{
        [dict setObject:self.userID forKey:@"userId"];
    }
    
    [dict setObject:[IVTAccountTool getPassword] forKey:@"captcha"];
    [dict setObject:[IVTAccountTool getUserName] forKey:@"username"];
    [dict setObject:@"1"  forKey:@"page"];
    [[IVTNetworkAPIClient sharedClient] getContributionListForUser:dict success:^(NSDictionary *successData) {
        [requestDataArray addObjectsFromArray:[successData valueForKey:@"data"]];
        if (requestDataArray.count == 0) {
            noDataLabel.hidden = NO;
        }
        else{
            noDataLabel.hidden = YES;
        if (requestDataArray!=nil) {
            for (int i=0; i<[requestDataArray count]; i++) {
                NSMutableDictionary *listDic =[[requestDataArray objectAtIndex:i] mutableCopy];
                [listDic setObject:[NSString stringWithFormat:@"%ld",[listDic[@"userId"] integerValue]] forKey:@"id"];
                [requestDataArray replaceObjectAtIndex:i withObject:[listDic copy]];
            }
        }
        }
        [_hintTable reloadData];
    } error:^(NSDictionary *successData) {
        
    } failure:^(NSError *failureError) {
        LRLog(@"failureError:%@",failureError);
    }];
}


#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return requestDataArray.count;
  
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row==0 || indexPath.row==1 || indexPath.row==2) {
        IVTMeSupportFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"supportFirstCell"];
        if (!cell) {
            cell = [[IVTMeSupportFirstCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"supportFirstCell"];
            cell.backgroundColor  =[UIColor IVTTitleColor];
            cell.selectionStyle  =UITableViewCellSelectionStyleNone;
            [cell addSubview:[self addLineViews]];
        }
        cell.entity = [requestDataArray objectAtIndex:indexPath.row];
        cell.goldImageName = imageArray[indexPath.row];
        return cell;
    }else{
        
        IVTMeSupportCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"supportCell"];
        if (!cell) {
            cell = [[IVTMeSupportCommonCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"supportCell"];
            cell.backgroundColor  =[UIColor IVTTitleColor];
            [cell addSubview:[self addLineView]];
            cell.selectionStyle  =UITableViewCellSelectionStyleNone;
        }
        cell.entity = [requestDataArray objectAtIndex:indexPath.row];
        
        return cell;
        
    }
}

#pragma mark - table delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0 || indexPath.row==2 || indexPath.row==1) {
        return 77;
        
    }else{
        
        return 59;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    IVTUserInfoCenterViewController *userInfo = [[IVTUserInfoCenterViewController alloc]init];
    userInfo.userId = [NSString stringWithFormat:@"%ld",[[requestDataArray[indexPath.row] valueForKey:@"userId"] integerValue]];
    [self.navigationController pushViewController:userInfo animated:YES];
}


- (UIView*)addLineView
{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 59.0-0.5, kScreenWidth, 0.5)];
    line.backgroundColor = [UIColor IVTSeparatorColor];
    return line;
}
- (UIView*)addLineViews
{
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 77.0-0.5, kScreenWidth, 0.5)];
    line.backgroundColor = [UIColor IVTSeparatorColor];
    return line;
}



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



@end