IOS 自定义 tableView cell(UITableViewCell height) 高度
来源:互联网 发布:广东省网络干部学院 编辑:程序博客网 时间:2024/06/09 19:54
IOS开发中,最常用的就是UITableView 了,这里,我做一个简单的帖子列表Demo,教大家用一种比较模块化的方法来自定义我们的cell 高度,废话不多说,我们开始新建一个工程。PS:采用纯代码的开发方式。
运行截图:
步骤:新建一个singleViewApplication
步骤:通过新建group把工程目录结构改成如下图所示
通过上面的图片能够看得出来,工程包含了View层、Model层和ViewController层,是一个典型的MVC开发架构。
model层有一个帖子模型,包含三个属性。.m文件不需要做任何实现。
步骤:
核心代码是在cell里面,先贴上一段.h文件的代码:
#import <UIKit/UIKit.h>#import "HQTieziModel.h"#define hqTableViewCellIdentify @"HQ_TABLE_VIEW_CELL"@interface HQTableViewCellData : NSObject@property (nonatomic, assign) BOOL showBottomLine;@property (nonatomic, assign) BOOL showTopLine;@property (nonatomic, assign) float cellHeight;@property (nonatomic, strong) HQTieziModel *tieziModel;@property (nonatomic, assign) CGRect titleFrame;@property (nonatomic, assign) CGRect descFrame;@property (nonatomic, assign) CGRect photoFrame;-(void)setUpCellData:(HQTieziModel *)tieziModel;@end@interface HQTableViewCell : UITableViewCell@property (nonatomic, strong) HQTableViewCellData *cellData;@end
-(void)setUpCellData:(HQTieziModel *)tieziModel{ CGFloat originX = 15; CGFloat originY = 15; CGSize titleSize = [self boundingString:tieziModel.tieziTitle rectWithSize:CGSizeMake(ScreenWidth - 30, FLT_MAX) withStringFont:titleFont]; self.titleFrame = CGRectMake(originX, originY, titleSize.width, titleSize.height); originY += titleSize.height; CGSize descSize = [self boundingString:tieziModel.tieziDesc rectWithSize:CGSizeMake(ScreenWidth - 30, FLT_MAX) withStringFont:descFont]; self.descFrame = CGRectMake(originX, originY + 15, descSize.width, descSize.height); originY += descSize.height + 15; CGFloat pictureHeight = 80; self.photoFrame = CGRectMake(originX, originY + 15 , pictureHeight, pictureHeight); originY += pictureHeight + 15 + 10; self.cellHeight = originY; self.tieziModel = tieziModel;}
//计算字符串高度方法- (CGSize)boundingString:(NSString *)text rectWithSize:(CGSize)size withStringFont:(UIFont *)font{ NSDictionary *attribute = @{NSFontAttributeName: font}; CGSize retSize = [text boundingRectWithSize:size options:\ NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; return retSize;}
核心代码就是上面那段了,完整demo请戳这里。
0 0
- IOS 自定义 tableView cell(UITableViewCell height) 高度
- iOS编程------自定义UITableViewCell / cell自适应高度
- IOS 自定义UITableViewCell:Cell高度、分割线、间距等
- iOS个人整理20-UITableViewCell自定义,cell高度的自适应(纯代码)
- iOS tableview自定义cell
- iOS 动态计算UITableViewCell 高度 以及计算UILable 高度 自定义Cell
- IOS swift 自定义Tableview cell
- Swift TableView自定义Cell的高度
- UI - TableView高级(自定义cell、cell自定义高度)
- IOS tableView cell动态高度 (autoLayout)
- ios tableview 自动计算cell高度
- [iOS]TableView的Cell高度自适应
- ios中自定义cell,自定义UITableViewCell
- iOS 自定义Cell 自适应高度
- iOS 自定义cell的高度
- iOS雨笙 lable自适应高度(多用于tableview的自定义cell中)
- iOS UITableViewCell重用以及自定义Cell
- 自定义UITableViewCell:Cell高度、分割线、间距等
- 【Qt OpenGL】Qt Creator中的3D绘图及动画教程
- IOS 填写密码
- virtualbox pxe启动
- HDU5336 XYZ and Drops 暴力模拟
- virtualenv
- IOS 自定义 tableView cell(UITableViewCell height) 高度
- linux下jdk的安装和配置
- 自己写的php curl库实现整站克隆
- MotionEvent 源码学习
- Linux(Ubuntu发行版本)下的几种软件安装方式
- POJ 1661 Help Jimmy(DP,注意边界)
- 袀芀薆薀羂肃蒂蕿肄芈莇蚈
- 蚈肆芆薄蚄袂膅薆蝿羁肄葿
- 袇膂膇蕿肂肈膆蚁袅羄膅螃