UIView not showing in both header views

落爺英雄遲暮 提交于 2019-12-25 10:17:02

问题


I am developing an iPad application. For some reasons, I need to show a two tableviews in the same view controller. I have created a UIView in a xib and connected a outlet to that view controller. I need to show that view in those two tableviews as a tableHeaderView. When I try to assign that view to both header views, it is showing only in the second tableview.I hope there is a single instance for that view. So it is showing on only in the view, which I assigned last. How could I overcome this?? Any help would be appreciated.

Here it is my code.

[energyTableView setTableHeaderView:tableHeader];
[maintenanceTableView setTableHeaderView:tableHeader];

@CodeBySteveZ

As he said, I created an xib and tried. And still I can't get the solution. Am I doing anything wrong??here it is how I tried.

NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"tableHeaderCell" owner:nil options:nil];

[energyTableView setTableHeaderView:[topLevelObjects objectAtIndex:0]];
[maintenanceTableView setTableHeaderView:[topLevelObjects objectAtIndex:0]];

I think this is also a same thing as I did before.


回答1:


You'll have to create another instance of the tableHeader. A UIView can only live in one visual sub-tree at a time. Using IB you could copy the element and create 2 instances of it. Or if you create a separate xib with that just element you could load that particular xib twice via code, once for each table header.




回答2:


@R.A I know its tedious to go programmatically but u can try this

 UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
 UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
 [headerView addSubview:labelView];
 energyTableView.tableHeaderView = headerView;

Similarly add to other tableview a den release it.

 [labelView release];
 [headerView release];

Hope it helps.




回答3:


one view can not appear in two places.... said simplified one view == one superview



来源:https://stackoverflow.com/questions/13322143/uiview-not-showing-in-both-header-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!