服务器之家

服务器之家 > 正文

详解iOS tableViewCell自适应高度 第三发类库

时间:2021-01-16 14:49     来源/作者:徒步天涯

在github中有许多大牛封装好的第三发类库,其中有个自适应cell高度的类库

详解iOS tableViewCell自适应高度 第三发类库

下载地址: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

 

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部