您的位置:首页 > 产品设计 > UI/UE

[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,可以自己对返回值进行修改。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: