[ios]UITableViewCell自适应高度 【转】
2014-01-24 08:31
399 查看
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字體進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font
constrainedToSize:CGSizeMake(contentWidth, 1000)
lineBreakMode:UILineBreakModeWordWrap];
// 這裏返回需要的高度
return size.height;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字體進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font
constrainedToSize:CGSizeMake(contentWidth, 1000)
lineBreakMode:UILineBreakModeWordWrap];
// 構建顯示行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
// 設置顯示榘形大小
rect.size = size;
// 重置列文本區域
cell.textLabel.frame = rect;
cell.textLabel.text = content;
// 設置自動換行(重要)
cell.textLabel.numberOfLines = 0;
// 設置顯示字體(一定要和之前計算時使用字體一至)
cell.textLabel.font = font;
return cell;
}
//===========//
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//这个frame是初设的,没关系,后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"abcdefghijklmn";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
////=============gengxing==========///
主要有两个地方需要调整高度,一个是自己创建的UILabel或其它,另一个就是cell的高度。在创建cell的地方只需要定义好label的属性就行了:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[contentLabel setLineBreakMode:UILineBreakModeWordWrap];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:FONT_CELL];
[contentLabel setTag:TAG_LABEL];
[[cell contentView] addSubview:contentLabel];
[contentLabel release];
}
设置完值后再设置frame:
cell.textLabel.text = @"";
CGSize captionSize = [cell.textLabel.text sizeWithFont:FONT_CELL];
contentLabel.frame = CGRectMake(captionSize.width + 10, 0,
CGRectGetWidth(cell.bounds) - captionSize.width - 10,
[self tableView:nil heightForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]);
我需要让cell的高度去适应label,所以调用计算cell高度的方法,在heightForRowAtIndexPath方法中计算label所需的高度即可:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row < …) {
return 90;
} else if (indexPath.row == ...) {
NSString *caption = @"";
CGSize contentSize = getTextSize(caption, FONT_CELL, @"data",
CGRectGetWidth(self.tableView.bounds));
return contentSize.height > 44 ? contentSize.height : 44;
}
return 44;
}
为了不让高度过于混乱,我把最小值设为44,getTextSize只是一个工具方法,用于计算x轴偏移的距离:
CGSize getTextSize(NSString *offsetText,UIFont *font,NSString *text, CGFloat maxWidth){
CGSize offsetTextSize = [offsetText sizeWithFont:font];
CGSize textSize = [text sizeWithFont:font
constrainedToSize:CGSizeMake(maxWidth-offsetTextSize.width, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
return textSize;
}
期间使用了一些公共宏,比如:FONT_CELL等等,只在cellForRowAtIndexPath里面设置好label的基本属性和frame,计算高度就交给heightForRowAtIndexPath,可以自己对返回值进行修改。
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字體進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font
constrainedToSize:CGSizeMake(contentWidth, 1000)
lineBreakMode:UILineBreakModeWordWrap];
// 這裏返回需要的高度
return size.height;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字體進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font
constrainedToSize:CGSizeMake(contentWidth, 1000)
lineBreakMode:UILineBreakModeWordWrap];
// 構建顯示行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
// 設置顯示榘形大小
rect.size = size;
// 重置列文本區域
cell.textLabel.frame = rect;
cell.textLabel.text = content;
// 設置自動換行(重要)
cell.textLabel.numberOfLines = 0;
// 設置顯示字體(一定要和之前計算時使用字體一至)
cell.textLabel.font = font;
return cell;
}
//===========//
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//这个frame是初设的,没关系,后面还会重新设置其size。
[label setNumberOfLines:0];
NSString *s = @"abcdefghijklmn";
UIFont *font = [UIFont fontWithName:@"Arial" size:12];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[label setFrame:CGRectMake(0,0, labelsize.width, labelsize.height)];
[self.view addSubView:label];
////=============gengxing==========///
主要有两个地方需要调整高度,一个是自己创建的UILabel或其它,另一个就是cell的高度。在创建cell的地方只需要定义好label的属性就行了:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier] autorelease];
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[contentLabel setLineBreakMode:UILineBreakModeWordWrap];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:FONT_CELL];
[contentLabel setTag:TAG_LABEL];
[[cell contentView] addSubview:contentLabel];
[contentLabel release];
}
设置完值后再设置frame:
cell.textLabel.text = @"";
CGSize captionSize = [cell.textLabel.text sizeWithFont:FONT_CELL];
contentLabel.frame = CGRectMake(captionSize.width + 10, 0,
CGRectGetWidth(cell.bounds) - captionSize.width - 10,
[self tableView:nil heightForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]);
我需要让cell的高度去适应label,所以调用计算cell高度的方法,在heightForRowAtIndexPath方法中计算label所需的高度即可:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row < …) {
return 90;
} else if (indexPath.row == ...) {
NSString *caption = @"";
CGSize contentSize = getTextSize(caption, FONT_CELL, @"data",
CGRectGetWidth(self.tableView.bounds));
return contentSize.height > 44 ? contentSize.height : 44;
}
return 44;
}
为了不让高度过于混乱,我把最小值设为44,getTextSize只是一个工具方法,用于计算x轴偏移的距离:
CGSize getTextSize(NSString *offsetText,UIFont *font,NSString *text, CGFloat maxWidth){
CGSize offsetTextSize = [offsetText sizeWithFont:font];
CGSize textSize = [text sizeWithFont:font
constrainedToSize:CGSizeMake(maxWidth-offsetTextSize.width, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
return textSize;
}
期间使用了一些公共宏,比如:FONT_CELL等等,只在cellForRowAtIndexPath里面设置好label的基本属性和frame,计算高度就交给heightForRowAtIndexPath,可以自己对返回值进行修改。
相关文章推荐
- iOS 客户端学习-UITableViewCell高度自适应
- (0072)iOS开发之UITableViewCell高度自适应探索--cell预估高度
- iOS UITableViewCell自适应高度单元格问题?
- iOS个人整理20-UITableViewCell自定义,cell高度的自适应(纯代码)
- iOS - UITableViewCell自适应高度
- iOS UILable自适应撑高即UITableViewCell的高度自适应改变
- [ios]UITableViewCell自适应高度 【转】
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
- iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
- 详解iOS tableViewCell自适应高度 第三发类库
- UITableViewCell自适应高度
- iOS UITableViewableViewCell自适应高度
- iOS 8 自适应 Cell,UITableViewAutomaticDimension设置无效,滚动后显示正常
- UITableViewCell高度自适应 autoLayout
- iOS 之cell自适应高度
- iOS cell 自适应高度
- UITableViewCell使用AutoLayout自适应高度
- 【转】UITableViewCell自适应高度
- IOS研究成果——在autolayout 如何实现 Cell 的高度自适应
- 【iOS开发】UITableView Cell 自适应高度