滚动时viewForHeaderInSection消失 [英] viewForHeaderInSection disappears when scrolling

查看:218
本文介绍了滚动时viewForHeaderInSection消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableView中使用了自定义标题视图,但是一旦我开始向下滚动,标题就会消失,而不是像默认标题一样停留在原处。而且它也不会滚动到屏幕之外,而只是彻底消失了。 viewForHeaderInSection仍在调用,但自定义标题视图的drawRect方法未调用。我究竟做错了什么?这是一些代码...

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 drawRect method of my custom header view isn't. What am I doing wrong? Here's some code...

我的UITableView数据源...

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));
}


推荐答案

我遇到了这个问题过去,为了避免这种情况,我通常在表委托/数据源上创建一个ivar,然后添加一个将对象保留在适当位置的属性。这样,以后滚动时就不会回收该对象,我什至可以从代码的其他部分访问和更改其值(根据应用程序可能实际上是必需的)。这样,您就不会放弃子类化和创建自己的标头的优点。

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.

这篇关于滚动时viewForHeaderInSection消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆