开发者

viewForHeaderInSection disappears when scrolling

开发者 https://www.devze.com 2022-12-28 15:00 出处:网络
I am using a custom header view in my UITableView, but as soon as i begin scrolling down the header disappears instead of staying in place like with the default headers. And it\'s not scrolling off th

I am using a custom header view in my UITableView, but as soon as i begin scrolling down the header disappears instead of staying in place like with the default headers. And it's not scrolling off the screen either, just completely disappering. viewForHeaderInSection is still being called but the d开发者_开发问答rawRect method of my custom header view isn't. What am I doing wrong? Here's some code...

My UITableView datasource...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{   
    HeaderView* headerView = [[[HeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)] autorelease];
    return headerView;
}

...and my custom header...

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 115.0f, 125.0f, 133.0f, 0.5f);
    CGContextFillRect(context, CGRectMake(0.0f, 0.0f, 320.0f, 40.0f));
}


I've had this problem in the past, and to avoid it I usually make an ivar on my table delegate / data source, and then I add a property that retains the object in place. This way, the object is not reclaimed later when scrolling, and I can even access and change its values from other parts of the code (which might actually be required depending on the app). This way you don't give up the advantages of subclassing and creating your own headers.

0

精彩评论

暂无评论...
验证码 换一张
取 消