// // KBChildFansViewController.m // YouHuiProject // // Created by 小花 on 2018/5/19. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBChildFansViewController.h" #import "KBChildFansCell.h" #import "KBChildFansModel.h" #import "KBFanRecommendController.h" @interface KBChildFansViewController () < UITableViewDelegate, UITableViewDataSource, YHChildFansDelegate > @property (nonatomic,strong) UIView *headerView; @property (nonatomic, strong) NSMutableArray *dataArr; @property (nonatomic) NSInteger page; @property (nonatomic) NSInteger member_type; @end @implementation KBChildFansViewController - (void)viewDidLoad { [super viewDidLoad]; [self configTableView]; [self request:NO]; } - (void)configTableView { self.page = 1; [self.view addSubview:self.headerView]; [self.view addSubview:self.tableView]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else { self.automaticallyAdjustsScrollViewInsets = NO; } } #pragma mark -------- UITableView Delegate ----- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 67; } //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ // return self.headerView; //} - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.1; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KBChildFansCell *cell = [KBChildFansCell cellWithTableView:tableView]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.updateAction = ^(KBChildFansModel *fanModel) { [self updateVipToSVip:fanModel]; }; KBChildFansModel *model = self.dataArr[indexPath.row]; cell.model = model; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.delegate = self; if (self.type == 0) { cell.sencondFansSumBtn.hidden = YES; } if (self.type == 0 || self.type == 2) { cell.updateBtn.hidden = YES; } return cell; } #pragma mark ------ private ----- - (void)updateVipToSVip:(KBChildFansModel *)fansModel { NSString *url = [NSString stringWithFormat:@"%@/api/v2/adzoneCreate/upUserLevel",BaseURL]; NSDictionary *para = @{@"user_id":fansModel.user_id}; [KBHttp post:url params:para success:^(id json) { NSNumber *flag = json[@"data"][@"flag"]; if (flag.integerValue == 1) { [self request:YES]; } [MBProgressHUD showMessage:json[@"data"][@"info"]]; } failure:^(NSError *error) { [MBProgressHUD showMessage:@"加载失败,请稍后再试"]; }]; } #pragma mark -代理 YHChildFansDelegate - (void)recommendFansByUserID:(NSString *)userId withUserName:(NSString *)userName{ KBFanRecommendController *vc=[[KBFanRecommendController alloc]init]; vc.userId=userId; vc.userName=userName; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark -网络请求 #pragma mark - request - (void)request:(BOOL)isChange { NSString *url=[NSString stringWithFormat:@"%@/api/v2/adzoneCreate/fansList",BaseURL]; NSDictionary *dic=@{ @"type":@(self.type), @"page":@(self.page), @"member_type":@(self.member_type) }; [KBHttp post:url params:dic success:^(id json) { NSArray *arr = [NSArray yy_modelArrayWithClass:[KBChildFansModel class] json:json[@"data"]]; if (isChange) { [self.dataArr removeAllObjects]; } if (arr.count>0) { [self.dataArr addObjectsFromArray:arr]; [self.tableView reloadData]; [self.tableView.mj_footer endRefreshing]; }else { [self setUpNoDataView]; [self noMoreDataWithArray:arr]; if (self.page==1) { [self.tableView reloadData]; } } UISegmentedControl *seg=[self.headerView viewWithTag:10001]; [seg setTitle:[NSString stringWithFormat:@"全部:%@位",json[@"fans_all"]] forSegmentAtIndex:0]; [seg setTitle:[NSString stringWithFormat:@"超级会员:%@位",json[@"fans_super"]] forSegmentAtIndex:1]; [seg setTitle:[NSString stringWithFormat:@"会员:%@位",json[@"fans_simple"]] forSegmentAtIndex:2]; [self.tableView.mj_header endRefreshing]; } failure:^(NSError *error) { [self.tableView.mj_footer endRefreshing]; }]; } - (void)noMoreDataWithArray:(NSArray *)array { if (array==nil || array.count <= 0) { [self.tableView.mj_footer endRefreshingWithNoMoreData]; } } - (void)setUpNoDataView { self.tableView.showNoDataView = YES; self.tableView.noDataImageOffsetY = -70; self.tableView.defaultNoDataText = @"您还没有粉丝记录"; self.tableView.defaultNoDataImage = [UIImage imageNamed:@"noData"]; } #pragma mark -事件 -(void)segAction{ UISegmentedControl *segment=[self.headerView viewWithTag:10001]; self.member_type=segment.selectedSegmentIndex; [self request:YES]; } - (void)refreshData { self.page = 1; [self request:YES]; } #pragma mark ------- layzer ------ - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 48, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-88) style:UITableViewStylePlain]; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.sectionFooterHeight = 0; _tableView.sectionHeaderHeight = 0; _tableView.estimatedRowHeight = 0; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.backgroundColor = [UIColor yhGrayColor]; _tableView.bounces = YES; _tableView.showsVerticalScrollIndicator = NO; _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; _tableView.separatorColor = [UIColor YHColorWithHex:0xEEEEEE]; MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{ [self refreshData]; }]; header.lastUpdatedTimeLabel.hidden = YES; header.stateLabel.hidden = YES; //正在刷新的图片 NSArray *imgArr = @[[UIImage imageNamed:@"ant1"],[UIImage imageNamed:@"ant2"]]; [header setImages:imgArr duration:0.3 forState:MJRefreshStateRefreshing]; [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStateWillRefresh]; [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStatePulling]; _tableView.mj_header = header; _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ self.page ++; [self request:NO]; }]; } return _tableView; } -(NSMutableArray *)dataArr{ if (!_dataArr) { _dataArr=[NSMutableArray array]; } return _dataArr; } -(UIView *)headerView{ if (!_headerView) { _headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 48)]; _headerView.backgroundColor=[UIColor yhGrayColor]; NSArray *array = [NSArray arrayWithObjects:@"全部:--位",@"超级会员:--位",@"会员:--位", nil]; UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array]; segment.frame=CGRectMake(0, 0, _headerView.width-2*FITSIZE(20), 28); segment.center=CGPointMake(_headerView.width/2, _headerView.height/2); segment.layer.borderColor=[UIColor YHColorWithHex:0xFF8600].CGColor; segment.layer.borderWidth=1; segment.layer.cornerRadius = 15.0f; segment.layer.masksToBounds = YES; segment.tag=10001; // [segment addTarget:self action:@selector(segAction) forControlEvents:UIControlEventTouchUpInside]; [[segment rac_signalForControlEvents:UIControlEventValueChanged] subscribeNext:^(id x) { UISegmentedControl *segment=(UISegmentedControl *)x; if (self.member_type!=segment.selectedSegmentIndex) { self.page=1; [self.dataArr removeAllObjects]; } self.member_type=segment.selectedSegmentIndex; [self request:YES]; }]; //选中的颜色 segment.tintColor=[UIColor YHColorWithHex:0xFF8600]; [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected]; //未选中的颜色 segment.selectedSegmentIndex=0; [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal]; [segment setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0f]} forState:UIControlStateNormal]; [_headerView addSubview:segment]; } return _headerView; } @end