-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentTableView.m
143 lines (96 loc) · 4.11 KB
/
CommentTableView.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
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
//
// CommentTableView.m
// WeiChi
//
// Created by imac on 15/9/16.
// Copyright (c) 2015年 imac. All rights reserved.
//
#import "CommentTableView.h"
#import "WeiboViewLayoutFrame.h"
@implementation CommentTableView
static NSString *cellId = @"cellId";
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
self = [super initWithFrame:frame style:style];
if (self) {
[self _creatHeaderView];
self.delegate = self;
self.dataSource = self;
UINib *nib = [UINib nibWithNibName:@"CommentCell" bundle:[NSBundle mainBundle]];
[self registerNib:nib forCellReuseIdentifier:cellId];
}
return self;
}
- (void)_creatHeaderView{
//1.创建父视图
_theTableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 0)];
_theTableHeaderView.backgroundColor = [UIColor clearColor];
//2.加载xib创建用户视图
_userView = [[[NSBundle mainBundle] loadNibNamed:@"UserView" owner:self options:nil] lastObject];
_userView.backgroundColor = [UIColor clearColor];
_userView.width = kScreenWidth;
_userView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.1];
[_theTableHeaderView addSubview:_userView];
//3.创建微博视图
_weiboView = [[WeiboView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
_weiboView.imgView.contentMode = UIViewContentModeScaleAspectFit;
[_theTableHeaderView addSubview:_weiboView];
}
- (void)setWeiboModel:(WeiboModel *)weiboModel{
if (_weiboModel != weiboModel) {
_weiboModel = weiboModel;
//1.创建微博视图的布局对象
WeiboViewLayoutFrame *layoutframe = [[WeiboViewLayoutFrame alloc] init];
//isDetail 需要先赋值
layoutframe.isDetail = YES;
layoutframe.weiboModel = weiboModel;
_weiboView.layoutFrame = layoutframe;
_weiboView.frame = layoutframe.frame;
_weiboView.top = _userView.bottom + 5;
//2.用户视图
_userView.weiboModel = weiboModel;
//3.设置头视图
_theTableHeaderView.height = _weiboView.bottom;
self.tableHeaderView = _theTableHeaderView;
}
}
#pragma mark - TabelView 代理
//获取组的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//1.创建组视图
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, 40)];
//2.评论Label
UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
countLabel.backgroundColor = [UIColor clearColor];
countLabel.font = [UIFont boldSystemFontOfSize:16.0f];
countLabel.textColor = [UIColor blackColor];
//3.评论数量
NSNumber *total = [self.commentDic objectForKey:@"total_number"];
int value = [total intValue];
countLabel.text = [NSString stringWithFormat:@"评论:%d",value];
[sectionHeaderView addSubview:countLabel];
sectionHeaderView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.1];
return sectionHeaderView;
}
//设置组的头视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
//设置单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CommentModel *model = self.commentDataArray[indexPath.row];
//计算单元格的高度
CGFloat height = [CommentCell getCommentHeight:model];
return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
cell.commentModel = self.commentDataArray[indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.commentDataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
@end