gpt4 book ai didi

iphone - 如何根据行内容制作可变的表格高度大小?

转载 作者:行者123 更新时间:2023-11-28 22:52:04 24 4
gpt4 key购买 nike

我希望行高基于内容高度...因此每个行高的大小都根据内容调整。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return tableView.contentSize.height;
}

这是我目前所拥有的,但它不起作用。

最佳答案

Try this code for exmaple and also refer this http://www.icodeblog.com/2010/11/18/making-smarter-table-view-cells/  

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 125.0f
#define CELL_CONTENT_MARGIN 10.0f
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// static NSString *CellIdentifier = @"Cell";
NSString *CellIdentifier = [NSString stringWithFormat:@"%d", indexPath.row] ;
UILabel *lblTitle = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;




lblTitle =[[UILabel alloc]initWithFrame:CGRectMake(5, 15, 110, 44)];
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.font = [UIFont systemFontOfSize:11.0];
lblTitle.tag = 1;
lblTitle.numberOfLines = 0;


UILabel *lblType =[[UILabel alloc]initWithFrame:CGRectMake(125, 0, 75, 44)];
lblType.backgroundColor = [UIColor clearColor];
lblType.textColor = [UIColor whiteColor];
lblType.font = [UIFont systemFontOfSize:11.0];
lblType.tag = 2;

UILabel *lblDate =[[UILabel alloc]initWithFrame:CGRectMake(200, 0, 60, 44)];
lblDate.backgroundColor = [UIColor clearColor];
lblDate.textColor = [UIColor whiteColor];
lblDate.font = [UIFont systemFontOfSize:11.0];
lblDate.tag = 3;

UILabel *lblTime =[[UILabel alloc]initWithFrame:CGRectMake(265, 0, 50, 44)];
lblTime.backgroundColor = [UIColor clearColor];
lblTime.textColor = [UIColor whiteColor];
lblTime.font = [UIFont systemFontOfSize:11.0];
lblTime.tag = 4;

[tableView beginUpdates];
[tableView endUpdates];
[cell.contentView addSubview:lblTitle];
[cell.contentView addSubview:lblType];
[cell.contentView addSubview:lblDate];
[cell.contentView addSubview:lblTime];

}

// lblTitle = (UILabel *) [cell.contentView viewWithTag:1];
// lblTitle.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"];
// NSString *strTitle =[NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];


// lblTitle.text = [NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];

NSString *text = [NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];


CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeCharacterWrap];

if (!lblTitle)
lblTitle = (UILabel*)[cell viewWithTag:1];

[lblTitle setText:text];
[lblTitle setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];


UILabel *lblType = (UILabel *) [cell.contentView viewWithTag:2];
lblType.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"type"];

UILabel *lblDate = (UILabel *) [cell.contentView viewWithTag:3];
lblDate.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"date"];

UILabel *lblTime = (UILabel *) [cell.contentView viewWithTag:4];
lblTime.text =[[itemsData objectAtIndex:indexPath.row] objectForKey:@"time"];

return cell;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [NSString stringWithFormat:@"%@,%@,%@",[[itemsData objectAtIndex:indexPath.row] objectForKey:@"address"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"title"],[[itemsData objectAtIndex:indexPath.row] objectForKey:@"zipcode"]];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeCharacterWrap];

CGFloat height = MAX(size.height, 44.0f);

return height + (CELL_CONTENT_MARGIN * 2);
}

关于iphone - 如何根据行内容制作可变的表格高度大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11730190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com