CollectionView.m
3.34 KB
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
//
// 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];
_isEnableSelection = true;
_selectedIndex = 0;
}
return self;
}
-(void) awakeFromNib {
[super awakeFromNib];
[self.collectionView registerNib:[UINib nibWithNibName:@"LabelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LabelCell"];
}
-(void) disableSelection {
_isEnableSelection = false;
_selectedIndex = -1;
[self.collectionView reloadData];
}
-(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 {
if(_isEnableSelection) {
_selectedIndex = indexPath.row;
[collectionView reloadData];
}
if(self.changeCurrentIndex != NULL) {
self.changeCurrentIndex(_selectedIndex);
}
}
@end