ios - viewForHeaderInSection 滚动时消失

标签 ios objective-c iphone uitableview cocoa-touch

我在我的 UITableView 中使用自定义标题 View ,但是一旦我开始向下滚动标题就会消失,而不是像默认标题那样留在原地。而且它也没有滚出屏幕,只是完全消失了。 viewForHeaderInSection 仍在被调用,但我的自定义标题 View 的 drawRect 方法不是。我究竟做错了什么?这是一些代码...

我的 UITableView 数据源...

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

...和我的自定义 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));
}

最佳答案

我过去遇到过这个问题,为了避免它,我通常在我的表委托(delegate)/数据源上创建一个 ivar,然后我添加一个属性来保留该对象。这样,稍后滚动时不会回收该对象,我什至可以从代码的其他部分访问和更改它的值(这实际上可能是必需的,具体取决于应用程序)。这样您就不会放弃子类化和创建自己的 header 的优势。

关于ios - viewForHeaderInSection 滚动时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2706207/

相关文章:

iOS 6+ : UITableViewController embedded into Navigation Controller shows table's first section header under status bar when Navigation Bar is hidden

ios - 如何在 iOS 中为 UITextField 提供填充?

ios - 控制台在加载 UIWebView 时显示 AutoLayout Engine 警告,有时会使应用程序崩溃

iphone - iOS - CAKeyFrameAnimation - 不播放

ios - 用 2 行截断 UILabel 的头部

ios - Objective-c 以正确的顺序使用字母数字字符串对 NSArray 进行排序

iphone游戏控制帮助

iphone - beginAnimations 和 using animations block 之间的动画速度不同

iphone - 删除 Game Center 应用程序中我的游戏图标的光泽

iphone - 一个接一个地运行动画失败