iOS中Masonry和UITableView+FDTemplateLayoutCell结合使用
程序员文章站
2022-03-24 13:38:06
ios中masonry和uitableview+fdtemplatelayoutcell结合使用。masonry的github链接:https://github.com/snapkit/masonr...
ios中masonry和uitableview+fdtemplatelayoutcell结合使用。masonry的github链接:https://github.com/snapkit/masonry
uitableview-fdtemplatelayoutcell github:https://github.com/forkingdog/uitableview-fdtemplatelayoutcell
注意:图片及json数据来自uitableview-fdtemplatelayoutcell的demo里面的。
具体代码如下:
一:控制器中创建视图,处理数据
// // viewcontroller.m // bjttest // // created by yunlong on 17/4/28. // copyright ? 2017年 yunlong. all rights reserved. // #import "viewcontroller.h" #import "uitableview+fdtemplatelayoutcell.h" #import "fdfeedentity.h" #import "fdfeedcell.h" @interface viewcontroller () @property(nonatomic,strong) uitableview *mytableview; //数据源 @property (nonatomic, strong) nsmutablearray *feedentitysections; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.mytableview = [[uitableview alloc] initwithframe:self.view.frame]; [self.view addsubview:self.mytableview]; self.mytableview.datasource = self; self.mytableview.delegate = self; //注册cell [self.mytableview registerclass:[fdfeedcell class] forcellreuseidentifier:@"fdfeedcell"]; self.mytableview.estimatedrowheight = 200;//预算行高 self.mytableview.fd_debuglogenabled = yes;//开启log打印高度 [self buildtestdatathen:^{ //刷新数据 [self.mytableview reloaddata]; }]; } #pragma mark - 处理数据 - (void)buildtestdatathen:(void (^)(void))then{ // simulate an async request dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ // data from `data.json` nsstring *datafilepath = [[nsbundle mainbundle] pathforresource:@"data" oftype:@"json"]; nsdata *data = [nsdata datawithcontentsoffile:datafilepath]; nsdictionary *rootdict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:nil]; nsarray *feeddicts = rootdict[@"feed"]; // convert to `fdfeedentity` nsmutablearray *entities = @[].mutablecopy; [feeddicts enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) { [entities addobject:[[fdfeedentity alloc] initwithdictionary:obj]]; }]; self.feedentitysections = entities; // callback dispatch_async(dispatch_get_main_queue(), ^{ !then ?: then(); }); }); } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ fdfeedcell *cell = [tableview dequeuereusablecellwithidentifier:@"fdfeedcell" forindexpath:indexpath]; [self configurecell:cell atindexpath:indexpath]; return cell; } - (void)configurecell:(fdfeedcell *)cell atindexpath:(nsindexpath *)indexpath{ cell.fd_enforceframelayout = no; // enable to use "-sizethatfits:" if (indexpath.row % 2 == 0) { cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } else { cell.accessorytype = uitableviewcellaccessorycheckmark; } cell.entity = self.feedentitysections[indexpath.row]; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath{ //高度计算并且缓存 return [tableview fd_heightforcellwithidentifier:@"fdfeedcell" cachebyindexpath:indexpath configuration:^(fdfeedcell *cell) { [self configurecell:cell atindexpath:indexpath]; }]; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ return self.feedentitysections.count; } @end
二:自定义cell及代码的实现
(1).h文件
#import @class fdfeedentity; @interface fdfeedcell : uitableviewcell @property (nonatomic, strong) fdfeedentity *entity; @end
(2).m文件
#import "fdfeedcell.h" #import "fdfeedentity.h" #import #define margin 4 #define padding 10 @interface fdfeedcell () @property (nonatomic, strong) uilabel *titlelabel; @property (nonatomic, strong) uilabel *contentlabel; @property (nonatomic, strong) uiimageview *mainimageview; @property (nonatomic, strong) uilabel *usernamelabel; @property (nonatomic, strong) uilabel *timelabel; @end @implementation fdfeedcell - (instancetype)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier{ if (self == [super initwithstyle:style reuseidentifier:reuseidentifier]) { [self setautolayout]; } return self; } #pragma mark - 布局 - (void)setautolayout{ _titlelabel = [[uilabel alloc] init]; _titlelabel.numberoflines = 0; //_titlelabel.backgroundcolor = [uicolor redcolor]; [self.contentview addsubview:_titlelabel]; _contentlabel = [[uilabel alloc] init]; _contentlabel.numberoflines = 0; _contentlabel.font = [uifont systemfontofsize:14]; _contentlabel.textcolor = [uicolor graycolor]; //_contentlabel.backgroundcolor = [uicolor purplecolor]; [self.contentview addsubview:_contentlabel]; _mainimageview = [[uiimageview alloc] init]; _mainimageview.contentmode = uiviewcontentmodescaleaspectfill; _mainimageview.clipstobounds = yes; //_mainimageview.backgroundcolor = [uicolor orangecolor]; [self.contentview addsubview:_mainimageview]; _usernamelabel = [[uilabel alloc] init]; //_usernamelabel.backgroundcolor = [uicolor greencolor]; _usernamelabel.textcolor = [uicolor orangecolor]; _usernamelabel.font = [uifont systemfontofsize:12]; [self.contentview addsubview:_usernamelabel]; _timelabel = [[uilabel alloc] init]; _timelabel.textcolor = [uicolor bluecolor]; _timelabel.font = [uifont systemfontofsize:12]; //_timelabel.backgroundcolor = [uicolor bluecolor]; [self.contentview addsubview:_timelabel]; [_titlelabel mas_makeconstraints:^(masconstraintmaker *make) { make.top.and.left.mas_equalto(self.contentview).offset(padding); make.right.mas_equalto(self.contentview.mas_right).offset(-padding); }]; [_contentlabel mas_makeconstraints:^(masconstraintmaker *make) { make.left.and.right.mas_equalto(self.titlelabel); make.top.mas_equalto(self.titlelabel.mas_bottom).offset(margin); }]; [_mainimageview mas_makeconstraints:^(masconstraintmaker *make) { make.left.equalto(self.titlelabel.mas_left); make.top.mas_equalto(self.contentlabel.mas_bottom).offset(margin); }]; [_usernamelabel mas_makeconstraints:^(masconstraintmaker *make) { make.left.equalto(self.titlelabel.mas_left); make.top.mas_equalto(self.mainimageview.mas_bottom).offset(margin); make.bottom.mas_equalto(self.contentview.mas_bottom).offset(-margin); }]; [_timelabel mas_makeconstraints:^(masconstraintmaker *make) { make.top.and.bottom.mas_equalto(self.usernamelabel); make.right.mas_equalto(self.titlelabel.mas_right); }]; } #pragma mark - 赋值 - (void)setentity:(fdfeedentity *)entity{ _entity = entity; self.titlelabel.text = entity.title; self.contentlabel.text = entity.content; self.mainimageview.image = entity.imagename.length > 0 ? [uiimage imagenamed:entity.imagename] : nil; self.usernamelabel.text = entity.username; self.timelabel.text = entity.time; }
三:model就是用uitableview-fdtemplatelayoutcell的demo里面的model
(1).h文件
#import @interface fdfeedentity : nsobject - (instancetype)initwithdictionary:(nsdictionary *)dictionary; @property (nonatomic, copy) nsstring *identifier; @property (nonatomic, copy) nsstring *title; @property (nonatomic, copy) nsstring *content; @property (nonatomic, copy) nsstring *username; @property (nonatomic, copy) nsstring *time; @property (nonatomic, copy) nsstring *imagename; @end
(1).m文件
#import "fdfeedentity.h" @implementation fdfeedentity - (instancetype)initwithdictionary:(nsdictionary *)dictionary { self = super.init; if (self) { _identifier = [self uniqueidentifier]; _title = dictionary[@"title"]; _content = dictionary[@"content"]; _username = dictionary[@"username"]; _time = dictionary[@"time"]; _imagename = dictionary[@"imagename"]; } return self; } - (nsstring *)uniqueidentifier { static nsinteger counter = 0; return [nsstring stringwithformat:@"unique-id-%@", @(counter++)]; } @end
五:效果图
推荐阅读
-
深入分析iOS应用中对于图片缓存的管理和使用
-
iOS中的音频服务和音频AVAudioPlayer音频播放器使用指南
-
深入分析iOS应用中对于图片缓存的管理和使用
-
iOS应用UI开发中的字体和按钮控件使用指南
-
iOS开发中Date Picker和UITool Bar控件的使用简介
-
解析iOS应用的UI开发中懒加载和xib的简单使用方法
-
在iOS开发的Quartz2D使用中实现图片剪切和截屏功能
-
Android中的Shape和Selector的结合使用实例
-
iOS中Masonry和UITableView+FDTemplateLayoutCell结合使用
-
mybatis xml文件中传入参数和if结合使用时要注意的地方