简体   繁体   English

UITableView标题覆盖(UIView)表现奇怪到滚动

[英]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. 我覆盖了(这是一个真正的单词吗?无论如何)我的UITableView标题,在整个应用程序中使用了常用的UIView。 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 实际上是UIView

TableSectionView.h 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 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. 这个应用程序有大约25个单独的表视图这个标题是分开的,我真的想要尽快压缩这个渲染错误。

I wanted to post a screenshot, but due to a strict NDA, I am not allowed to without running risk of being fired. 我想发布一个截图,但由于严格的NDA,我不被允许没有被解雇的风险。 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. 黑条是节标题,图像的顶部是UITableView的顶部。 Notice how much that section header is DOWN, vs where it is supposed to be? 请注意该节标题是DOWN,与它应该在哪里相比? (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. 在layoutSubviews中添加子视图很奇怪。 Why don't you do it in the initWithFrame to avoid confusing the rendering code of Cocoa? 为什么不在initWithFrame中执行此操作以避免混淆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. 编辑2:当你初始化UILabel你应该使用self.bounds而不是self.frame作为参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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