RankDetailViewController.m 15.6 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9
//
//  RankDetailViewController.m
//  redstar
//
//  Created by admin on 15/10/29.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "RankDetailViewController.h"
10 11
#import "RankDetailTableCell.h"
#import "RankDetailHeaderView.h"
admin's avatar
admin committed
12

13
#import "RankHeadView.h"
admin's avatar
admin committed
14 15 16 17 18 19 20 21 22
#import <MBProgressHUD.h>
#import "HttpClient.h"

#import "TaskGroup.h"
#import "TaskModel.h"
#import "TaskDetailModel.h"
#import "CheckPicViewController.h"

#import <UIButton+WebCache.h>
admin's avatar
admin committed
23

24 25 26
#define kRankDetailCell @"RankDetailTableCell"

@interface RankDetailViewController () <UITableViewDataSource, UITableViewDelegate, RankHeadViewDelegate>
admin's avatar
admin committed
27
@property (nonatomic, strong) UITableView *tableView;
28

29 30
@property (nonatomic, strong) RankDetailHeaderView *rankDetailHeaderView;

admin's avatar
admin committed
31
@property (nonatomic, strong) NSMutableArray *taskData;
32

33 34
@property (nonatomic, strong) NSMutableDictionary *selectedIndexes; // 存放二次展开的cell

admin's avatar
admin committed
35 36 37
@end

@implementation RankDetailViewController
admin's avatar
admin committed
38 39 40 41 42 43 44
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}
admin's avatar
admin committed
45 46 47 48 49 50 51

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
admin's avatar
admin committed
52 53 54 55 56 57 58
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.translucent = NO;
    }
    
admin's avatar
admin committed
59 60 61 62 63 64
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"口碑详情"];
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
65 66 67 68 69 70 71 72
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;

    
73 74
    // 初始化容器
    self.selectedIndexes = [[NSMutableDictionary alloc] init];
admin's avatar
admin committed
75 76
    // 请求口碑排名详情
    [self requestRankingDetail];
admin's avatar
admin committed
77 78 79 80 81 82 83 84 85

}

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

#pragma mark - Private Methods
admin's avatar
admin committed
86 87 88 89 90 91

- (void)requestRankingDetail
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@/%@",kRedStarURL, kInspectDetailURL, self.uuid, self.store_uuid];
    NSLog(@"url = %@" , url);
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
92
    
admin's avatar
admin committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    [httpClient getPraiseDetailWithParameters:nil completion:^(id response, NSError *error) {
        NSLog(@"口碑巡检明细PraiseDetail = %@", response);
        NSDictionary *dataDict = response[@"data"];
        TaskDetailModel *taskDetail = [TaskDetailModel taskDetailModelWithDict:dataDict];
        self.rankDetailHeaderView.taskDetail = taskDetail;

        NSArray *dataArray = dataDict[@"categories"];
        
        NSMutableArray *tgArray = [NSMutableArray array];
        for (NSDictionary *dict in dataArray) {
            TaskGroup *taskGroup = [TaskGroup taskGroupWithDict:dict];
            [tgArray addObject:taskGroup];
        }
        
        _taskData = tgArray;

        
        self.tableView.tableHeaderView = self.rankDetailHeaderView;
        [self.tableView registerClass:[RankDetailTableCell class] forCellReuseIdentifier:kRankDetailCell];
        [self setupScrollView];
    }];

admin's avatar
admin committed
115 116
}

admin's avatar
admin committed
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupScrollView
{
    int imageCount = 5;
    CGFloat marginW = 5.0;
    CGFloat imageViewW = 90 + 2 * marginW;
    CGFloat imageViewH = self.rankDetailHeaderView.rankDetailView.showScrollView.bounds.size.height - 4;
    self.rankDetailHeaderView.rankDetailView.showScrollView.contentSize = CGSizeMake(imageCount *imageViewW, imageViewH + 4);
    for (int i = 0; i < imageCount; i++) {
        UIButton *button = [[UIButton alloc] init];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"0%d", i]] forState:UIControlStateNormal];
        button.frame = CGRectMake(i * imageViewW + 2, 2, imageViewW - 2 * marginW, imageViewH);
admin's avatar
admin committed
135
        [button addTarget:self action:@selector(showBigPic:) forControlEvents:UIControlEventTouchUpInside];
136 137 138 139
        [self.rankDetailHeaderView.rankDetailView.showScrollView addSubview:button];
    }
}

admin's avatar
admin committed
140
- (void)showBigPic:(UIButton *)btn
141
{
admin's avatar
admin committed
142 143 144
    CheckPicViewController *checkVC = [[CheckPicViewController alloc] init];
    checkVC.checkImage = btn.imageView.image;
    [self.navigationController pushViewController:checkVC animated:YES];
145 146 147
}


148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
- (void)showScrollView:(UIButton *)sender
{
    RankDetailTableCell * cell = (RankDetailTableCell *)[[sender superview] superview];
    cell.isDrop = !cell.isDrop;
    NSIndexPath * path = [self.tableView indexPathForCell:cell];
    
    [_tableView deselectRowAtIndexPath:path animated:TRUE];
    
    BOOL isSelected = ![self cellIsSelected:path];
    
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [_selectedIndexes setObject:selectedIndex forKey:path];
    
    if (cell.isDrop) {
        [UIView animateWithDuration:0.2 animations:^{
            cell.arrowImageView.transform = CGAffineTransformIdentity;
            cell.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
        }];
admin's avatar
admin committed
166 167 168 169 170 171 172 173 174
        NSArray *imgaeUrlArray = cell.task.answerAttachments;
        NSMutableArray *urlArray = [NSMutableArray array];
        for (NSDictionary *imageUrlDict in imgaeUrlArray) {
            NSString *urlStr = [NSString stringWithFormat:@"%@%@", kRedStarURL, imageUrlDict[@"fileUrl"]];
            [urlArray addObject:urlStr];
        }
        
        CGFloat marginW = 5.0;
        CGFloat imageViewW = 90 + 2 * marginW;
175 176 177
        
        cell.scrollView = [[UIScrollView alloc] init];
        cell.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
admin's avatar
admin committed
178
        cell.scrollView.contentSize = CGSizeMake((urlArray.count - 1) * imageViewW, 65);
179 180
        [cell.contentView addSubview:cell.scrollView];
        
admin's avatar
admin committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        
        for (int i = 0; i < urlArray.count; i++) {
            UIButton *button = [[UIButton alloc] init];
            button.translatesAutoresizingMaskIntoConstraints = NO;
            NSString *urlStr = [NSString stringWithFormat:@"%@", urlArray[i]];
            NSURL *url = [NSURL URLWithString:urlStr];
            [button sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default_pic"]];
            [button addTarget:self action:@selector(showBigPic:) forControlEvents:UIControlEventTouchUpInside];
            [cell.scrollView addSubview:button];
            
            
            NSLayoutConstraint *urlTop = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.scrollView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
            [cell.scrollView addConstraint:urlTop];
            
            NSLayoutConstraint *urlLeft = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell.scrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:i * imageViewW];
            [cell.scrollView addConstraint:urlLeft];
            
            NSLayoutConstraint *urlWidth = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:imageViewW];
            [cell.scrollView  addConstraint:urlWidth];
            
            NSLayoutConstraint *urlHeight = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:65];
            [cell.scrollView addConstraint:urlHeight];
        }
        
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        NSLayoutConstraint *someoneTop = [NSLayoutConstraint constraintWithItem:cell.scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:45];
        [cell.contentView addConstraint:someoneTop];
        
        NSLayoutConstraint *someoneLeft = [NSLayoutConstraint constraintWithItem:cell.scrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
        [cell.contentView addConstraint:someoneLeft];
        
        NSLayoutConstraint *someoneRight = [NSLayoutConstraint constraintWithItem:cell.scrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
        [cell.contentView addConstraint:someoneRight];
        
        NSLayoutConstraint *someoneHeight = [NSLayoutConstraint constraintWithItem:cell.scrollView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:65];
        [cell.contentView addConstraint:someoneHeight];
        
    } else {
        [UIView animateWithDuration:0.2 animations:^{
            cell.arrowImageView.transform = CGAffineTransformIdentity;
            cell.arrowImageView.transform = CGAffineTransformMakeRotation(0);
        }];
        
        if (cell.scrollView) {
            [cell.scrollView removeFromSuperview];
            cell.scrollView = nil;
        }
    }
    
    [_tableView beginUpdates];
    [_tableView endUpdates];
}


- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
    // Return whether the cell at the specified index path is selected or not
    NSNumber *selectedIndex = [_selectedIndexes objectForKey:indexPath];
    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}

admin's avatar
admin committed
240 241 242
#pragma mark - UITableView DataSource/Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
admin's avatar
admin committed
243
    return _taskData.count;
admin's avatar
admin committed
244 245 246 247
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
admin's avatar
admin committed
248 249
    TaskGroup *taskGroup = _taskData[section];
    NSInteger count = taskGroup.isOpened ? taskGroup.answers.count : 0;
250
    return count;
admin's avatar
admin committed
251 252 253 254 255
}

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
256 257 258
    RankDetailTableCell *cell=[tableView dequeueReusableCellWithIdentifier:kRankDetailCell];
    if (!cell) {
        cell = [[RankDetailTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kRankDetailCell];
admin's avatar
admin committed
259
    }
260
    
admin's avatar
admin committed
261 262 263 264
    TaskGroup *taskGroup = _taskData[indexPath.section];
    TaskModel *task = taskGroup.answers[indexPath.row];
    
    cell.task = task;
265
    
266 267
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

admin's avatar
admin committed
268 269
    [cell.mainTitleBtn setTitle:[NSString stringWithFormat:@"%d、%@", (int)(indexPath.row + 1) ,task.title] forState:UIControlStateNormal];
    cell.scoreLabel.text = [NSString stringWithFormat:@"%@分", task.score];
270 271
    // 按钮点击事件
    [cell.mainTitleBtn addTarget:self action:@selector(showScrollView:) forControlEvents:UIControlEventTouchUpInside];
272 273
    
    return cell;
274

admin's avatar
admin committed
275 276 277 278 279 280 281 282 283 284 285
}

// cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
286 287 288 289 290
    if([self cellIsSelected:indexPath]) {
        return 130;
    }
    
    return 44;
admin's avatar
admin committed
291 292 293 294 295
}

// section高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
296
    return 50;
admin's avatar
admin committed
297 298 299 300 301 302 303 304 305 306 307
}

// foot高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

// 自定义section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
308 309
    RankHeadView *headView = [RankHeadView headViewWithTableView:tableView];
    headView.delegate = self;
admin's avatar
admin committed
310
    headView.taskGroup = _taskData[section];
311
    return headView;
admin's avatar
admin committed
312 313
}

314 315 316 317 318 319
- (void)clickRankHeadView
{
    [self.tableView reloadData];
}


admin's avatar
admin committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
#pragma mark - lazy Laoding
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_tableView];
        
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [self.view addConstraint:tableTop];
        
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
        
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
        
        NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableBottom];
    }
    return _tableView;
}


348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
- (RankDetailHeaderView *)rankDetailHeaderView
{
    if (!_rankDetailHeaderView) {
        _rankDetailHeaderView = [[RankDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 298)];
        if (self.indexRow < 3) {
            NSString *imageName = [NSString stringWithFormat:@"medal_0%ld", (long)(_indexRow + 1)];
            _rankDetailHeaderView.rankDetailView.gradeImageView.image = [UIImage imageNamed:imageName];
        } else {
            NSString *rankStr = [NSString stringWithFormat:@"第 %ld 名", (long)(_indexRow + 1)];
            NSMutableAttributedString *rankAttr = [[NSMutableAttributedString alloc] initWithString:rankStr];
            [rankAttr addAttributes:@{NSForegroundColorAttributeName:kRankHeadTitleTextColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} range:NSMakeRange(0,1)];
            [rankAttr addAttributes:@{NSForegroundColorAttributeName:kGradeNumberTextColor} range:NSMakeRange(1,rankStr.length - 2)];
            [rankAttr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:18.0] range:NSMakeRange(1,rankStr.length - 2)];
            [rankAttr addAttributes:@{NSForegroundColorAttributeName:kRankHeadTitleTextColor,NSFontAttributeName:[UIFont systemFontOfSize:14.0f]} range:NSMakeRange(rankStr.length - 1,1)];
            [_rankDetailHeaderView.rankDetailView.gradeLabel setAttributedText:rankAttr];
        }
    }
    return _rankDetailHeaderView;
}

admin's avatar
admin committed
368 369 370 371 372 373 374 375 376 377 378
/*
#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