仅当部分中包含行时才显示tableView标头

时间:2011-11-10 13:34:57

标签: iphone uitableview ios5

我的表有两个部分,在第一部分我没有总是有元素;

我已将此条件if([tableView numberOfRowsInSection:section]<=0) return 0;置于我的viewForHeaderInSection方法中但我的应用程序仍然需要EXC_BAD_ACCESS

这是我自定义的headerView:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    if([myTableView numberOfRowsInSection:section]<=0) return 0;

    NSString *headerTitle = [titlesArray objectAtIndex:section];
    UIView *customView = [ [ [UIView alloc] initWithFrame: CGRectMake(0.0, -5.0, 320.0, 25.0) ] autorelease ];
    [customView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myTableBackground.png"]]];
    UIView *clearView = [ [ [ UIView alloc ] initWithFrame: CGRectMake(0.0, -5.0, 320.0, 25.0) ] autorelease ];
    [clearView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:nil]]];
    UIButton *button = [ [ [ UIButton alloc ] initWithFrame: CGRectMake(-9.0, 5.0, 320, 27.0) ] autorelease ];
    button.tag = section;
    [button setImage:[UIImage imageNamed:@"header_section_background.png" ] forState: UIControlStateNormal ];
    button.highlighted = NO;
    button.adjustsImageWhenHighlighted = NO;
    UILabel *label = [ [ UILabel alloc ] initWithFrame:CGRectMake(15.0, 5.0, 45, 22.0) ];
    label.alpha = 1.0;
    label.backgroundColor = [UIColor clearColor];

    label.text = title;
    label.textAlignment = UITextAlignmentRight;

    [customView addSubview: button ];
    [customView addSubview:label ];
    [customView sendSubviewToBack:button];

    return customView;
}

有任何线索如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您应该返回nil而不是0

您可能/应该使用tableView参数而不是myTableView变量。