-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnswerCollectionView.m
93 lines (77 loc) · 2.4 KB
/
AnswerCollectionView.m
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
//
// AnswerCollectionView.m
// unnamed
//
// Created by Casing Chu on 2/20/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//
#import "AnswerCollectionView.h"
#import "AnswerView.h"
#import "Answer.h"
@interface AnswerCollectionView ()
@property (nonatomic, strong) NSArray *answers;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, strong) NSMutableArray *answerViews;
@property (nonatomic, assign) CGFloat answerHeight;
@end
@implementation AnswerCollectionView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)baseInit {
self.answerViews = [[NSMutableArray alloc] init];
self.total = 0;
self.answerHeight = 18.0;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self baseInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self baseInit];
}
return self;
}
- (void)setAnswers:(NSArray *)answers andTotal:(NSInteger)total {
_answers = answers;
_total = total;
for (AnswerView *view in self.answerViews) {
[view removeFromSuperview];
}
[self.answerViews removeAllObjects];
int count = 1;
for (Answer *answer in self.answers) {
AnswerView *view = [[AnswerView alloc] init];
view.index = count;
view.total = self.total;
view.answer = answer;
count++;
[self.answerViews addObject:view];
[self addSubview:view];
}
[self setNeedsLayout];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,
self.answerHeight * self.answerViews.count);// +1 to account additional height
for (int i=0;i<self.answerViews.count;i++) {
AnswerView *view = self.answerViews[i];
// NSLog(@"Height: %f, Width: %f", view.frame.size.height, view.frame.size.width);
// NSLog(@"Height: %f, Width: %f", self.frame.size.height, self.frame.size.width);
CGFloat y = self.answerHeight * i;
// NSLog(@"y pos: %f", y);
CGRect imageFrame = CGRectMake(0, y, self.frame.size.width, self.answerHeight);
view.frame = imageFrame;
}
}
@end