CollectionView.m 3.06 KB
//
//  CollectionView.m
//  LifeLog
//
//  Created by nvtu on 8/3/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import "CollectionView.h"
#import "LabelCollectionViewCell.h"
#import "Utilities.h"

@implementation CollectionView

-(id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        [[UINib nibWithNibName:@"CollectionView" bundle:nil] instantiateWithOwner:self options:nil];
        [self addSubview:self.view];
        self.view.frame = self.bounds;
        _number = 0;
        _spacing = 1;
        _cornerRadius = 5.0;
        _highlightColor = [Utilities convertHecToColor:0x999999];
        _normalColor = [UIColor whiteColor];
        _textColor = [UIColor blackColor];
    }
    return self;
}

-(void) awakeFromNib {
    [super awakeFromNib];
    [self.collectionView registerNib:[UINib nibWithNibName:@"LabelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LabelCell"];
}

-(void) setButtonNumber: (NSInteger) number {
    _number = number;
}

-(void) setSpacing: (float) spacing {
    _spacing = spacing;
}

-(void) setCornerRadius: (float) radius {
    _cornerRadius = radius;
}

-(void) setSelectedIndex: (int) index {
    _selectedIndex = index;
    [_collectionView reloadData];
}

-(void) setNormalColor: (UIColor *) normal highlightColor: (UIColor *) highlight textColor: (UIColor *) text {
    _normalColor = normal;
    _highlightColor = highlight;
    _textColor = text;
}

-(void) setArrayTitle: (NSArray *) title {
    _arrayTitle = title;
}

-(int) getCurrentIndex {
    return _selectedIndex;
}

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

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _number;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LabelCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LabelCell" forIndexPath:indexPath];
    cell.lblTitle.text = [_arrayTitle objectAtIndex:indexPath.row];
    cell.lblTitle.textColor = _textColor;
    cell.lblTitle.layer.cornerRadius = _cornerRadius;
    cell.lblTitle.backgroundColor = (indexPath.row == _selectedIndex) ? _highlightColor : _normalColor;
    return cell;
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((collectionView.frame.size.width - (_spacing * _number)) / _number, collectionView.frame.size.height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return _spacing;
}

-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _selectedIndex = indexPath.row;
    [collectionView reloadData];
    self.changeCurrentIndex(_selectedIndex);
}
@end