在github中有许多大牛封装好的第三发类库,其中有个自适应cell高度的类库
下载地址:https://github.com/gsdios/sdautolayout
model类
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
commentsmodel
#import "jsonmodel.h"
#import "getcommentdata.h"
@interface commentsmodel : jsonmodel
@property(nonatomic,copy)nsarray<getcommentdata> *commentlist;
@end
#import "commentsmodel.h"
@implementation commentsmodel
@end
getcommentdata
#import "jsonmodel.h"
@protocol getcommentdata
@end
@interface getcommentdata : jsonmodel
@property(nonatomic,copy)nsstring *message;
@property(nonatomic,copy)nsstring *nickname;
@property(nonatomic,copy)nsstring *createtimestr;
@end
#import "getcommentdata.h"
@implementation getcommentdata
@end
|
控制器
?
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
|
#import "commentstableviewcontroller.h"
#import "commentsmodel.h"
#import "commentcell.h"
@interface commentstableviewcontroller ()
@property(nonatomic,strong)nsarray *commentsarray;
@end
@implementation commentstableviewcontroller
-(nsarray *)commentsarray{
if (_commentsarray==nil) {
nsdata *data=[nsdata datawithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "comment_list.json" oftype:nil]];
commentsmodel *commensm=[[commentsmodel alloc]initwithdata:data error:nil];
_commentsarray=commensm.commentlist;
}
return _commentsarray;
}
- ( void )viewdidload {
[super viewdidload];
}
- ( void )didreceivememorywarning {
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
#pragma mark - table view data source
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {
return 1;
}
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
return self.commentsarray.count;
}
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
static nsstring *id=@ "comment" ;
commentcell *cell=[tableview dequeuereusablecellwithidentifier:id];
if (cell==nil) {
cell=[[commentcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:id];
}
cell.commentdata=self.commentsarray[indexpath.row];
return cell;
}
-(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{
return [self cellheightforindexpath:indexpath cellcontentviewwidth:[self cellcontentviewwith]];
}
-(cgfloat)cellcontentviewwith{
cgfloat width=[uiscreen mainscreen].bounds.size.width;
if ([uiapplication sharedapplication].statusbarorientation != uiinterfaceorientationportrait && [[uidevice currentdevice].systemversion floatvalue] < 8) {
width = [uiscreen mainscreen].bounds.size.height;
}
return width;
}
@end
|
具体自定义cell的代码
?
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
|
#import <uikit/uikit.h>
@ class getcommentdata;
@interface commentcell : uitableviewcell
@property(nonatomic,strong)getcommentdata *commentdata;
@property(nonatomic,strong)uilabel *namelabel;
@property(nonatomic,strong)uilabel *titlelabel;
@property(nonatomic,strong)uilabel *datelabel;
@end
#import "commentcell.h"
#import "commentsmodel.h"
@implementation commentcell
-( void )setcommentdata:(getcommentdata *)commentdata{
_commentdata=commentdata;
_titlelabel.text=commentdata.message;
_datelabel.text=commentdata.createtimestr;
_namelabel.text=commentdata.nickname;
}
- (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier
{
self = [super initwithstyle:style reuseidentifier:reuseidentifier];
if (self) {
[self setup];
}
return self;
}
-( void )setup{
_namelabel=[uilabel new ];
[self.contentview addsubview:_namelabel];
_namelabel.textcolor=[uicolor colorwithred:0.891 green:0.549 blue:0.073 alpha:1.000];
_namelabel.font=[uifont systemfontofsize:15];
_namelabel.numberoflines=1;
_titlelabel=[uilabel new ];
[self.contentview addsubview:_titlelabel];
_titlelabel.textcolor=[uicolor darkgraycolor];
_titlelabel.font=[uifont systemfontofsize:15];
_titlelabel.numberoflines=0;
_datelabel=[uilabel new ];
[self.contentview addsubview:_datelabel];
_datelabel.textcolor=[uicolor colorwithred:0.679 green:0.166 blue:0.828 alpha:1.000];
_datelabel.font=[uifont systemfontofsize:15];
_datelabel.numberoflines=1;
cgfloat margin=10;
uiview *contentview=self.contentview;
_namelabel.sd_layout
.leftspacetoview(contentview,margin)
.topspacetoview(contentview,margin)
.rightspacetoview(contentview,margin)
.heightis(20);
_titlelabel.sd_layout
.leftspacetoview(contentview,margin)
.topspacetoview(_namelabel,2)
.rightspacetoview(contentview,margin)
.autoheightratio(0);
_datelabel.sd_layout
.leftspacetoview(contentview,margin)
.topspacetoview(_titlelabel,5)
.heightis(20)
.widthis(150);
[self setupautoheightwithbottomviewsarray:@[_titlelabel,_datelabel,_namelabel] bottommargin:margin];
}
- ( void )awakefromnib {
}
- ( void )setselected:( bool )selected animated:( bool )animated {
[super setselected:selected animated:animated];
}
@end
|
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-26 20
-
2025-05-27 20
-
2025-05-29 64
-
2025-05-25 59
-
2025-06-04 83
热门评论