UIView not showing in both header views

2019-08-29 01:46发布

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.

3条回答
太酷不给撩
2楼-- · 2019-08-29 02:17

@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楼-- · 2019-08-29 02:30

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.

查看更多
Bombasti
4楼-- · 2019-08-29 02:34

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

查看更多
登录 后发表回答