【问题标题】:iPhone - UITableViewController - Setting section header in portrait and landscapeiPhone - UITableViewController - 在纵向和横向设置节标题
【发布时间】:2011-02-27 14:04:06
【问题描述】:

我有 UITableView 控制器,它使用自定义部分标题视图。当我的设备是纵向的时,它的显示很完美。当我的设备处于横向状态时,它再次完美。但是当应用程序运行时方向发生变化时,它不会更新我的剖面视图。有什么问题?

使用以下代码,我向视图添加了一个标签和两个按钮,并在“viewForHeaderInSection”中返回该视图。在调试过程中,我发现该方法被调用,但节标题视图没有更新。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary* dict = [threadsArray objectAtIndex:section];

    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
    headerView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.2];

    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, self.view.frame.size.width - 98, 21)];
    newLabel.textColor = [UIColor blackColor];
    newLabel.font = [UIFont boldSystemFontOfSize:17];
    newLabel.text = [dict objectForKey:@"Name"];
    newLabel.backgroundColor = [UIColor clearColor];
    [headerView addSubview:newLabel];

    UIButton* addButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
    addButton.frame = CGRectMake(self.view.frame.size.width - 88, 11, 29, 29);
    NSLog(@"%f %f %f %f",addButton.frame.origin.x,addButton.frame.origin.y, addButton.frame.size.width,addButton.frame.size.height);
    [addButton addTarget:self action:@selector(addThreadResponseClicked:) forControlEvents:UIControlEventTouchDown];
    [headerView addSubview:addButton];

    UIButton* expandCollapseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [expandCollapseButton setImage:[UIImage imageNamed:@"Expand.png"] forState:UIControlStateNormal];
    expandCollapseButton.frame = CGRectMake(self.view.frame.size.width - 48, 11, 29, 29);
    NSLog(@"%f %f %f %f",expandCollapseButton.frame.origin.x,expandCollapseButton.frame.origin.y, expandCollapseButton.frame.size.width,expandCollapseButton.frame.size.height);
    [expandCollapseButton addTarget:self action:@selector(expandCollapseHeader:) forControlEvents:UIControlEventTouchDown];
    [headerView addSubview:expandCollapseButton];

    addButton.tag = expandCollapseButton.tag = section;
    return [headerView autorelease];
}

【问题讨论】:

    标签: iphone uitableview sectionheader


    【解决方案1】:

    尝试适当地设置视图autoresizing masks:例如对于headerView;

    [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    

    对于 addButton 和 expandCollapseButton,将 AutoresizingMask 设置为:

    UIViewAutoresizingMaskFlexibleLeftMargin
    

    【讨论】:

    • 如果你看到我的代码,当方向改变时,我正在创建新的 headerView。即使我尝试设置自动调整大小的蒙版。这对我没有帮助。
    • 而不是使用边界来引用视图的框架,通常在添加子视图时使用边界,因为当 UI 旋转时,框架不一定会切换宽度和高度(它是有点随意,我希望苹果能修复)
    • 设置自动调整大小掩码解决了我的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 2014-06-03
    • 2013-04-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多