InspectRepairAddPicTableViewCell.m 5.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
//
//  RepairAddPicTableViewCell.m
//  patrol
//
//  Created by Javen on 2016/10/23.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "InspectRepairAddPicTableViewCell.h"
#import "RepairAddPicCollectionViewCell.h"
#import "TZImagePickerController.h"
#import "GalleryViewController.h"
@interface InspectRepairAddPicTableViewCell () <UICollectionViewDelegate,
                                                UICollectionViewDataSource>

@end
@implementation InspectRepairAddPicTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.layOut.itemSize = kPicCellSize;
    self.layOut.minimumInteritemSpacing = 10;
    self.layOut.minimumLineSpacing = 10;
    self.layOut.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // Initialization code
}

- (void)configCellWithViewModel:(PicViewModel *)viewModel indexPath:(NSIndexPath *)indexPath target:(UIViewController *)target {
    self.vc = target;
    self.viewModel = viewModel;
    //如果是只读的,则直接刷新。
    if (self.viewModel.type == kPicCellTypeRead) {
        [self.collectionView reloadData];
        return;
    }

    //非只读情况,则是添加或者编辑
    if (self.viewModel.arrPics.count == kMaxAddRepairImgNumber) {
        self.viewModel.type = kPicCellTypeEdit;
    } else {
        self.viewModel.type = kPicCellTypeAdd;
    }
    [self.collectionView reloadData];
}

#pragma mark - collectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    switch (self.viewModel.type) {
        case kPicCellTypeAdd: {
            return self.viewModel.arrPics.count + 1;
            break;
        }
        default: {
            return self.viewModel.arrPics.count;
            break;
        }
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RepairAddPicCollectionViewCell *cell = [collectionView
        dequeueReusableCellWithReuseIdentifier:@"RepairAddPicCollectionViewCell"
                                  forIndexPath:indexPath];
    [cell configCellWithArr:self.viewModel.arrPics indexPath:indexPath];
    //删除
    cell.blockDelete = ^(NSInteger index) {

    };
    return cell;
}

#pragma mark - collectionView DataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    switch (self.viewModel.type) {
        case kPicCellTypeRead: {
            UIStoryboard *board = [UIStoryboard storyboardWithName:@"ZJPicture" bundle:nil];
            GalleryViewController *galleryVC = [board instantiateViewControllerWithIdentifier:@"GalleryViewController"];
            galleryVC.arrData = [self.viewModel.arrPics mutableCopy];
            galleryVC.page = indexPath.item;
            [self.vc.navigationController pushViewController:galleryVC animated:YES];
            break;
        }
        default: {
            if (indexPath.row == self.viewModel.arrPics.count) { //说明点击的是添加图片的那个按钮
                [self pushImagePickerController];
            } else { //点击查看大图
                [self seePicsWithIndex:indexPath.item];
            }
            break;
        }
    }
}

#pragma mark - TZImagePickerController

- (void)pushImagePickerController {
    CGFloat canSelectNum = kMaxAddRepairImgNumber;
    TZImagePickerController *imagePickerVc =
        [[TZImagePickerController alloc] initWithMaxImagesCount:canSelectNum - self.viewModel.arrPics.count delegate:nil];
    imagePickerVc.isSelectOriginalPhoto = NO;
    imagePickerVc.navigationBar.barTintColor = kMainColor;
    imagePickerVc.oKButtonTitleColorNormal = kMainColor;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.showSelectBtn = YES;
    WS(weakSelf);
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [weakSelf.viewModel.arrPics addObjectsFromArray:photos];
        [weakSelf.collectionView reloadData];
        weakSelf.blockReloadData();
    }];

    [self.vc presentViewController:imagePickerVc animated:YES completion:nil];
}

/**
 查看大图

 @param index 点击中的图片下标
 */
- (void)seePicsWithIndex:(NSInteger)index {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"ZJPicture" bundle:nil];
    GalleryViewController *galleryVC = [board instantiateViewControllerWithIdentifier:@"GalleryViewController"];
    galleryVC.arrData = self.viewModel.arrPics;
    galleryVC.page = index;
    galleryVC.type = GalleryTypeEdit;
    [self.vc.navigationController pushViewController:galleryVC animated:YES];
    WS(weakSelf);
    galleryVC.blockDelete = ^(NSInteger index) {

        [weakSelf.collectionView reloadData];
        weakSelf.blockReloadData();
    };
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end