iOS tableView adds subviews under the cell

    [self.tableView reloadData];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableView insertSubview:self.header_backgroundView atIndex:0];
    });

First, after the tableView is refreshed, call the [self.tableView insertSubview:self.header_backgroundView atIndex:0]; method,

The effect is shown in the figure

Then, listen to the scroll event of the tableView, insert the subview to the bottom layer during the scrolling process,

        [scrollView vv_addObserver:scrollViewHelper forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew withBlock:^(NSDictionary * _Nonnull change, void * _Nonnull context) {
            [scrollViewHelper scrollViewDidSroll:weakScrollView superViewInsetHeight:offset];
            [weakScrollView insertSubview:headerConfig.backgroundView atIndex:0];
        }];

This is a bit different from collectionView, you need to constantly mobilize the method during tableveiw scrolling, otherwise it will cause the cell to be overwritten

If it is not inserted into the bottom layer in the monitoring or scrolling proxy method, the effect after scrolling is as shown in the figure

Guess you like

Origin blog.csdn.net/LIUXIAOXIAOBO/article/details/112884174