iOS 动态计算UITableViewCell 高度 以及计算UILable 高度 自定义Cell
来源:互联网 发布:淘宝刷单 青青岛 编辑:程序博客网 时间:2024/05/29 05:05
UITableView 用于实现功能菜单。
类似于微信,微博,以及大众点评等等一些功能菜单;
个人的基本信息:包括 我的相册,我的收藏,名字,性别等等一些基础信息都是用UITableView 实现;所以写了个组件,用于以后的项目使用;
当中遇到计算UITableViewCell的高度以及计算UILable 高度;
首先定义Cell属性:
//// SJCellProperties.h// HappyApp//// Created by LiShijia on 14/12/21.// Copyright (c) 2014年 com.johnli. All rights reserved.//#import <Foundation/Foundation.h>@interface SJCellProperties : NSObject//名称@property (nonatomic,retain) NSString *functionName;//对应的value@property (nonatomic,retain) NSString *functionValue;//logo地址@property (nonatomic,retain) NSString *functionLogo;//是否有logo@property (nonatomic,assign) BOOL isHaveLogo;//后续还可以扩展属性@end
自定义Cell
//// SJTableViewCell.m// HappyApp//// Created by LiShijia on 14/12/21.// Copyright (c) 2014年 com.johnli. All rights reserved.//#import "SJTableViewCell.h"#import "SJCellProperties.h"@implementation SJTableViewCell-(id) initWithStyleNormal:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; _functionName = [[UILabel alloc] initWithFrame:CGRectMake(15,17, 80, 20)];// _value = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-200, 5, 100, 100)]; [self.contentView addSubview:_functionName];// [self.contentView addSubview:_value]; self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return self;}-(id) initWithStyleLogo:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; _functionName = [[UILabel alloc] initWithFrame:CGRectMake(40,17, 80, 20)]; _functionLogo = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17, 22, 22)]; _functionLogo.image = [UIImage imageNamed:@"big.png"]; _functionValue = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width-150, 17, 150, 20)]; [self.contentView addSubview:_functionLogo]; [self.contentView addSubview:_functionName]; [self.contentView addSubview:_functionValue]; _functionValue.textAlignment = UITextAlignmentRight; _functionValue.numberOfLines = 0; _functionValue.lineBreakMode = NSLineBreakByCharWrapping; _functionValue.font = [UIFont systemFontOfSize:16.0]; self.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return self;}-(void) layoutSubviews{ [super layoutSubviews]; NSString *functionValue = self.functionValue.text; CGSize valueSize = [functionValue sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(150, 10000000) lineBreakMode:NSLineBreakByWordWrapping]; CGRect rect = self.functionValue.frame; rect.size.height = valueSize.height; self.functionValue.frame = rect;}@end
使用自定义Cell
//// UserInfoViewController.m// HappyApp//// Created by LiShijia on 14/12/21.// Copyright (c) 2014年 com.johnli. All rights reserved.//#import "UserInfoViewController.h"#import "SJTableViewCell.h"#import "SJCellProperties.h"@implementation UserInfoViewController-(void) viewDidLoad{ [self.view setBackgroundColor:[UIColor redColor]]; self.title = @"UserInfo"; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; //初始化section;功能 [self initFunctions]; }-(void) initFunctions{ SJCellProperties *userHead = [[SJCellProperties alloc] init]; [userHead setFunctionName:@"头像"]; SJCellProperties *nickName = [[SJCellProperties alloc] init]; [nickName setFunctionName:@"昵称"]; [nickName setFunctionValue:@"johnLIddddretertretreddgdfgdfggdfgdfgddd的等待等待的的"]; SJCellProperties *sex = [[SJCellProperties alloc] init]; [sex setFunctionName:@"性别"]; [sex setFunctionValue:@"男"]; SJCellProperties *address = [[SJCellProperties alloc] init]; [address setFunctionName:@"常住地"]; [address setFunctionValue:@"深圳"]; _firstSection = [NSArray arrayWithObjects:userHead,nickName,sex,address, nil]; _arraySection = [NSArray arrayWithObjects:_firstSection, nil]; }-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSArray *array = [_arraySection objectAtIndex:section]; if (section ==0) { return array.count; }else{ return array.count; }}-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identify = @"cell"; SJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify]; if(!cell){ cell = [[SJTableViewCell alloc] initWithStyleLogo:UITableViewCellStyleDefault reuseIdentifier:identify]; } NSArray *array = [_arraySection objectAtIndex:indexPath.section]; SJCellProperties *cellProperties = [array objectAtIndex:indexPath.row]; cell.functionName.text = cellProperties.functionName; cell.functionValue.text = cellProperties.functionValue; //此处可以计算UILable的高度// NSString *functionValue = cellProperties.functionValue;// // CGSize valueSize = [functionValue sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(150, 10000000) lineBreakMode:NSLineBreakByWordWrapping];// // CGRect rect = cell.functionValue.frame;// rect.size.height = valueSize.height;// cell.functionValue.frame = rect;// // NSLog(@"%.f",valueSize.height); // [cell.beforeTitle setText:@"qqqqqqqqq"];// [cell.textLabel setText:@"tetst"]; return cell; }-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 16;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSArray *array = [_arraySection objectAtIndex:indexPath.section]; SJCellProperties *cellProperties = [array objectAtIndex:indexPath.row]; //动态计算Cell的高度 NSString *functionValue = cellProperties.functionValue; CGSize valueSize = [functionValue sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(150, 10000000) lineBreakMode:NSLineBreakByWordWrapping]; if(valueSize.height + 20 < 50){ return 50; } return valueSize.height + 20;}@end
0 0
- iOS 动态计算UITableViewCell 高度 以及计算UILable 高度 自定义Cell
- UITableViewCell动态计算高度
- 动态计算UITableviewCell高度
- 计算动态UILable的高度
- 动态计算cell高度
- iOS cell 高度计算
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- iOS学习之-Cell动态高度计算
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- JAVA知识点总结(三)成员变量和局部变量的区别
- 求最小环(floyd,dijkstra)
- hdu_1013_A + B Problem II_(模拟)
- 4.2 File
- SQL还原数据库代码
- iOS 动态计算UITableViewCell 高度 以及计算UILable 高度 自定义Cell
- Redhat 6 安装mysql 5.6.22遇到的问题总结
- 谷歌“我的足迹”源码
- HPU1250: HH的米5 【并查集】+【欧拉图】
- DBInputFormat的用法
- 人生选择与程序算法
- LEETCODE:Unique Paths II
- UVA 11529 Strange Tax Calculation
- Pascal's Triangle