iphone - 使用计算大小动态加载 Tableview 标题

标签 iphone ios ios4

大家好我正在开发一个应用程序。我想显示一个带有标题 View 的 TableView 。当我从 Root View 导航到下一个 View ( TableView )时,如下所示。但我的问题是当我传递一些文本时从 Root View 到下一个 View ( TableView )作为下一个 View 的标题,它以适当的格式显示如下所示。我需要为此解决方案以显示一些更好的格式。下面是我用于此的代码。 enter image description here

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{

/*   NSString *str_header1;
 str_header1=[[NSString alloc]initWithString:@"Heading : "];
 str_header1=[str_header1 stringByAppendingFormat:str_code1];
 str_header1=[str_header1 stringByAppendingFormat:@" - "];
 str_header1=[str_header1 stringByAppendingFormat:str_header]; */


// Create label with section title
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 300, 20)];
UILabel *titleLabel = tmpTitleLabel;
titleLabel.font = [UIFont boldSystemFontOfSize:12];
titleLabel.numberOfLines = 0;
titleLabel.textColor = [UIColor colorWithRed:0.30 green:0.34 blue:0.42 alpha:1.0];
titleLabel.shadowColor = [UIColor whiteColor];
titleLabel.shadowOffset = CGSizeMake(0.0, 1.0);
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.text = str_combine;

//Calculate the expected size based on the font and linebreak mode of label
CGSize maximumLabelSize = CGSizeMake(300,9999);
expectedLabelSize= [str_combine sizeWithFont:titleLabel.font constrainedToSize:maximumLabelSize lineBreakMode:titleLabel.lineBreakMode];

//Adjust the label the the new height
CGRect newFrame = titleLabel.frame;
newFrame.size.height = expectedLabelSize.height;
titleLabel.frame = newFrame;



// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 320, expectedLabelSize.height+30)];
[view addSubview:titleLabel];

return view;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section 
{
return expectedLabelSize.height+30;
}

最佳答案

您的代码假定 -tableView:viewForHeaderInSection: 总是在 -tableView:heightForHeaderInSection: 之前调用,但事实并非如此。您可以将大小计算移动到 -tableView:heightForHeaderInSection: 来解决这个问题。

更好的是,因为您似乎只使用一个表头 View :UITableView 上有一个名为 tableHeaderView 的属性,您可以使用它。将方法 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 重命名为 -(UIView *)headerView,删除方法 - tableView:heightForHeaderInSection: 并在 View Controller 的 -viewDidLoad 方法末尾添加以下行:

self.tableView.tableHeaderView = [self headerView];

关于iphone - 使用计算大小动态加载 Tableview 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11185545/

相关文章:

android - 按位结构的大小错误

iphone - 如何将我的应用程序数据从一部 iPhone 发送到另一部

ios - 如何在 Swift 字典中对同一个值使用多个键?

ios - 动画持续时间 :delay:options:animations:completion: blocking UI when used with UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse

iphone - 是否有 iOS API 调用来访问 iPhone 上的 UMTS pdp 接口(interface)?

iphone - 应用平台规范

iphone - xCode Organizer 中的分发证书

ios - 在 ios 上使用不受信任的证书加密数据

iphone - IOS App验证错误: An SSL error has occurred and secure connection to the server cannot be made

iphone - 如何在文档中下载和保存 xml,然后如何从文档中解析 xml 文件?