iPhone - UITableViewController - 设置纵向和横向的节标题
我有 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];
}
I'm having UITableView controller which uses custom section header view. When my device is portrait, its showing perfect. And when my device is landscape its perfect again. But when orientation changed during app runtime, its not updating my section view. What's the problem?
Using following code, I'm adding a label, and two buttons to a view and returning that view in "viewForHeaderInSection". During debugging I found that the method is getting called, but the section header view is not updating.
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试设置视图 适当地自动调整遮罩大小:例如对于 headerView;
对于 addButton 和 ExpandCollapseButton 将 AutoresizingMask 设置为:
Try setting the views autoresizing masks appropriately: eg for headerView;
and for addButton and expandCollapseButton set the AutoresizingMask to: