#import "GJCell.h"#import "Masonry.h"staticNSString*constGJCellIndentifier=@"GJCell";@interfaceViewController()<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,strong)NSArray*datas;@property(nonatomic,weak)UITableView*tableView;@end@implementationViewController#pragma mark - Life Cycle-(void)viewDidLoad{[superviewDidLoad];[selfsetupData];[selfsetupView];}-(void)setupData{NSDictionary*data1=@{@"icon":@"myIcon",@"name":@"GJBlog",@"content":@"今天天气真好啊"};NSDictionary*data2=@{@"icon":@"myIcon",@"name":@"GJBlogGJBlogGJBlog",@"content":@"今天天气真好啊今天天气真好啊今天天气真好啊今天天气真好啊"};NSDictionary*data3=@{@"icon":@"myIcon",@"name":@"GJBlogGJBlogGJBlogGJBlogGJBlog",@"content":@"今天天气真好啊今天天气真好啊今天天气真好啊今天天气真好啊今天天气真好啊今天天气真好啊今天天气真好啊"};self.datas=@[data1,data2,data3];}-(void)setupView{UITableView*tableView=[[UITableViewalloc]initWithFrame:CGRectZerostyle:UITableViewStylePlain];[tableViewregisterClass:[GJCellclass]forCellReuseIdentifier:GJCellIndentifier];tableView.dataSource=self;tableView.delegate=self;[self.viewaddSubview:tableView];self.tableView=tableView;[tableViewmas_makeConstraints:^(MASConstraintMaker*make){make.edges.equalTo(self.view);}];}
#pragma mark - UITableViewDataSource-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return60;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{GJCell*cell=[tableViewdequeueReusableCellWithIdentifier:GJCellIndentifierforIndexPath:indexPath];[selfconfigureCell:cellatIndexPath:indexPath];returncell;}-(void)configureCell:(GJCell*)cellatIndexPath:(NSIndexPath*)indexPath{NSIntegerrow=indexPath.row%3;NSDictionary*data=self.datas[row];UIImage*image=[UIImageimageNamed:data[@"icon"]];[cell.customImageViewsetImage:image];[cell.titlesetText:data[@"name"]];[cell.subtitlesetText:data[@"content"]];}
实现UITableViewDelegate
12345678910111213141516171819202122
#pragma mark - UITableViewDelegate-(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{return[selfheightForCellAtIndexPath:indexPath];}-(CGFloat)heightForCellAtIndexPath:(NSIndexPath*)indexPath{staticGJCell*cell=nil;staticdispatch_once_tonceToken;dispatch_once(&onceToken,^{cell=[self.tableViewdequeueReusableCellWithIdentifier:GJCellIndentifier];});[selfconfigureCell:cellatIndexPath:indexPath];return[selfcalculateHeightForCell:cell];}-(CGFloat)calculateHeightForCell:(GJCell*)cell{[cellsetNeedsLayout];[celllayoutIfNeeded];CGSizesize=[cell.contentViewsystemLayoutSizeFittingSize:UILayoutFittingCompressedSize];returnsize.height+1.0f;}