IPhone - fixed (tableHeaderView) with grouped UITableView when scrolling cells

I have one UIViewwith a grouped one UITableView, and I would like it to UIViewstay still tableHeaderView, and not move when scrolling UITableViewCell. Therefore, I would like the cells to be behind UIViewwhen scrolling.

This is the code I'm using now to add mine UIViewto mine UIViewController:

Interface.h:

@interface MyAccountVC : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    HeaderView *myHeaderView; // Inherits from UIView
    UITableView *tv;
}

@property (nonatomic, retain) IBOutlet UITableView *tv;
@property (nonatomic, retain) HeaderView myHeaderView;

Implementation of .m (in viewDidLoad):

// Set up the table header view based on our UIView 'HeaderView' outlet.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil];

for (id currentObject in topLevelObjects) {
    if ([currentObject isKindOfClass:[UIView class]]) {
        self.myHeaderView = (HeaderView *)currentObject;
        break;
    }
}

CGRect newFrame = CGRectMake(0.0, 0.0, self.tv.bounds.size.width, self.myHeaderView.frame.size.height);
self.myHeaderView.frame = newFrame;

self.tv.tableHeaderView = self.myHeaderView;

Not sure if this is the best way to add myHeaderViewin UITableView, but it seems to work fine, and I can just add the above code to anyone UIViewControllerwith UITableViewuse a header if necessary.

, , UINavigationBar, . , , , , .

+3
3

.

nib, , , .

, .

:

- (void) loadView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    self.view = view;

    self.myHeaderView = ... // use your init function, with rect something like (0,0,320,50)
    [view addsubview:self.myHeaderView];

    self.tv = [[UITableView alloc] init... // with frame like (0,50,320,430)
    [view addSubview:self.tv];

    // release everything
}

, UIView, tableview.

myHeaderView headerview, .

, nib, , , viewDidLoad, UITableView (0, myHeaderView.frame.size.height).

EDIT * , 4 :)

+7

UIViewController UITableViewDelegate UITableViewDataSource.

UITableView UIViewController, UIView UITableView, -addSubview:(UIView *)view. UITableView .

:

[tableView setContentInset:UIEdgeInsetsMake(44.0f, 0.0f, 0.0f, 0.0f)];
[tableView setContentOffset:CGPointMake(0.0f, 44.0f)];
[[self view] addSubview:tableView];

UIView *viewAboveTableView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
[[self view] addSubview:viewAboveTableView];

UIImageView UIView, UITableView, , UIView. , UIView.

, Interface Builder. UIView Interface Builder, UITableView. - Interface Builder.

+2

You can have a view just above the UITableView instead of having it inside as a headerview.

0
source

Source: https://habr.com/ru/post/1796603/


All Articles