简体   繁体   中英

UITableView Header Override (UIView) behaving oddly to scrolling

I overrode (is that a real word? regardless) my UITableView headers with a common used UIView to be used across the app. Its no biggie, just a back solid background, pre-selected font, font size, font weight, font color, etc. Thats REALLY all there is too it. In fact heres the UIView

TableSectionView.h

#import <UIKit/UIKit.h>

@interface TableSectionView : UIView {
    NSString *textLabel;
    UIColor *textColor;
    UIColor *backgroundColor;
    BOOL bold;
    NSString *textFont;
    NSInteger textSize;
}


@property (nonatomic, retain) NSString *textLabel, *textFont;
@property (nonatomic, retain) UIColor *textColor, *backgroundColor;
@property (nonatomic) BOOL bold;
@property (nonatomic) NSInteger textSize;

- (id)initWithFrame:(CGRect)frame andText:(NSString*)text;

@end

TableSectionView.m

#import "TableSectionView.h"

@implementation TableSectionView

@synthesize textLabel, backgroundColor, textColor, bold, textFont, textSize;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame andText:(NSString *)text {

    self = [self initWithFrame:frame];
    if(self) {

        textLabel = text;
        textFont = @"Arial-BoldMT";
        textSize = 16;
        textColor = [[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:1];
        backgroundColor = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.8];

    }
    return self;

}

-(void)layoutSubviews {
    [super layoutSubviews];

    UILabel *title = [[UILabel alloc] initWithFrame:self.frame];

    title.text = [NSString stringWithFormat:@"  %@", textLabel];
    title.font = [UIFont fontWithName:textFont size:textSize];
    title.textColor = textColor;
    title.backgroundColor = backgroundColor;

    [self addSubview:title];

}

@end

and how i use it in a file:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[TableSectionView alloc] initWithFrame:CGRectZero andText:@"Table Section Title"];
}

Nothing strange or unique here.

And this works, theres no problem with it showing up just fine. What happens is when you scroll the table view. When you scroll up, the section header shifts down and stays there. The harder the velocity, the more the section header shifts down and stays there until you go back down to the top of the table. This app has about 25 separate table views this header is to be apart of, and I really want to squash this rendering bug asap.

I wanted to post a screenshot, but due to a strict NDA, I am not allowed to without running risk of being fired. But here is SOMETHING to visualize the problem. Ive blurred out all of the text, sorry I had no other choice :\\

在此输入图像描述

The black bar is the section header, the top of the image is the top of the UITableView. Notice how much that section header is DOWN, vs where it is supposed to be? (the top, like normal section headers). Im just not sure what to do here

I think your problem is here:

-(void)layoutSubviews {
    [super layoutSubviews];

    UILabel *title = [[UILabel alloc] initWithFrame:self.frame];

    title.text = [NSString stringWithFormat:@"  %@", textLabel];
    title.font = [UIFont fontWithName:textFont size:textSize];
    title.textColor = textColor;
    title.backgroundColor = backgroundColor;

    [self addSubview:title];

}

Adding subviews in layoutSubviews is odd. Why don't you do it in the initWithFrame to avoid confusing the rendering code of Cocoa?

EDIT: That also saves you from saving all those member vars, declaring all those properties & synthesizing them :)

EDIT 2: When you init the UILabel you should use as reference self.bounds instead of self.frame , too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM