-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentCollectionView.m
92 lines (73 loc) · 2.33 KB
/
CommentCollectionView.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
//
// CommentCollectionView.m
// unnamed
//
// Created by Casing Chu on 2/23/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//
#import "CommentCollectionView.h"
#import "CommentView.h"
@interface CommentCollectionView ()
@property (nonatomic, strong) NSArray *comments;
@property (nonatomic, strong) NSMutableArray *commentViews;
@property (nonatomic, assign) CGFloat commentHeight;
@end
@implementation CommentCollectionView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)setup {
self.commentViews = [[NSMutableArray alloc] init];
self.commentHeight = 20.0;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self setup];
}
return self;
}
- (void)setComments:(NSArray *)comments {
_comments = comments;
for (CommentView *view in self.commentViews) {
[view removeFromSuperview];
}
[self.commentViews removeAllObjects];
int count = 1;
for (int i=0;i<comments.count;i++) {
CommentView *view = [[CommentView alloc] init];
//fill in comment's content
count++;
[self.commentViews addObject:view];
[self addSubview:view];
}
[self setNeedsLayout];
}
- (void)layoutSubviews {
[super layoutSubviews];
int verticalOffset = 16;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,
self.commentHeight * self.commentViews.count +
(self.commentViews.count > 0 ? verticalOffset * 2 : 0));
for (int i=0;i<self.commentViews.count;i++) {
CommentView *view = self.commentViews[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.commentHeight * i + verticalOffset;
// NSLog(@"y pos: %f", y);
CGRect imageFrame = CGRectMake(0, y, self.frame.size.width, self.commentHeight);
view.frame = imageFrame;
}
}
@end