【问题标题】:UITableView header for section only appears when scrolling down部分的 UITableView 标题仅在向下滚动时出现
【发布时间】:2012-05-14 07:44:47
【问题描述】:

我在 iPad 上出现了一些在 iPhone 上没有的奇怪行为。

我有一个分组表视图,其中包含部分和部分的标题,问题是在 iPad 上最顶部的部分的标题没有显示,当向下滚动表格时,部分标题会在开始之前出现一小会儿屏幕。

滚动前

http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing

滚动后

​​>

http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing

创建节标题的代码:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil || [sectionTitle isEqualToString:@""]) {
        return nil;
    }

    // Create label with section title
    UILabel *label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(12, 0, 300, 30);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0.0, 1.0);
    label.font = [UIFont boldSystemFontOfSize:16];
    label.text = sectionTitle;

    UIImage *img = [UIImage imageNamed:@"header"];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
    imgView.image = img;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];

    [view addSubview:imgView];
    [view addSubview:label];

    return view;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
    return [self.groupHeadings objectAtIndex:aSection];
}

我的 TableView 的代码:

tableViewResult = [[UITableView alloc] initWithFrame:mainView.frame style:UITableViewStyleGrouped];

tableViewResult.separatorColor = [UIColor clearColor];
[mainView addSubview:tableViewResult];

我在另一种方法中设置委托和数据源,因为我在将任何数据加载到表中之前首先执行 Web 请求,即当 Web 请求完成时我会这样做:

tableViewResult.delegate = self;
tableViewResult.dataSource = self;
[tableViewResult reloadData];

除了最顶部部分的标题之外,一切都按预期工作,并且仅在 iPad 上。

有什么想法会导致这种行为吗?

【问题讨论】:

  • 如何从 Interface Builder 或代码中创建 TableView?标题的高度是多少?可以上传截图吗?
  • 我是用代码构建的,会上传截图。
  • 你有没有写出截面高度的方法。
  • @MangeshVyas 添加为节标题指定高度的方法显示标题,但它有很多额外的填充。
  • 在您的情况下,我认为您的滚动视图放置没有导航栏所需的空间。将您的滚动视图放置在 44 像素下方并查看。

标签: iphone ipad uitableview ios5


【解决方案1】:

解决问题的方法是将此函数更改为:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
    if([[self.groupHeadings objectAtIndex:aSection] isEqualToString:@""])
        return nil;
    return [self.groupHeadings objectAtIndex:aSection];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2014-11-11
    • 2018-10-25
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多