iOS uitableview部分标题不会保留在视图之上

时间:2012-10-13 17:05:41

标签: ios xcode uitableview header

正如标题所暗示的那样,我在表格中的部分标题,当滚动时不会保持在顶部。它以前工作但由于某种原因,它停止了。我无法弄清楚为什么我的生活。任何帮助都是极好的。感谢。

代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [xmlParser.calls count] * 2 + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

return headerHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row % 2 == 0)
return 7;
return 65;
}

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

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 20)];
headerView.backgroundColor = [UIColor colorWithRed:(51/255.0)  green:(54/255.0)  blue:(57/255.0)  alpha:1.0];

CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 44.0f, self.tableView.bounds.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor colorWithRed:(130/255.0)  green:(130/255.0)  blue:(130/255.0)  alpha:1.0].CGColor;
[headerView.layer addSublayer:bottomBorder];

CALayer *topBorder = [CALayer layer];
topBorder.frame = CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 1.0f);
topBorder.backgroundColor = [UIColor colorWithRed:(130/255.0)  green:(130/255.0)  blue:(130/255.0)  alpha:1.0].CGColor;
[headerView.layer addSublayer:topBorder];

CALayer *imageBorder = [CALayer layer];
imageBorder.frame = CGRectMake(0.0f, 45, self.tableView.bounds.size.width, 7);
imageBorder.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]].CGColor;
[headerView.layer addSublayer:imageBorder];

UILabel *countyLabel = [[UILabel alloc] initWithFrame:CGRectMake(136, 0, 75, 20)];
countyLabel.text = @"LEGEND";
countyLabel.backgroundColor = [UIColor clearColor];
countyLabel.font = [UIFont boldSystemFontOfSize:11];
countyLabel.textColor = [UIColor whiteColor];
[headerView addSubview:countyLabel];

UILabel *county = [[UILabel alloc] initWithFrame:CGRectMake(37, 20, 75, 20)];
county.text = @"WA. COUNTY";
county.backgroundColor = [UIColor clearColor];
county.font = [UIFont systemFontOfSize:10];
county.textColor = [UIColor whiteColor];
[headerView addSubview:county];

UIImageView *waCountyImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, 18, 18)];
waCountyImage.image = [UIImage imageNamed:@"blue.png"];
[headerView addSubview:waCountyImage];

UILabel *county1 = [[UILabel alloc] initWithFrame:CGRectMake(128, 20, 75, 20)];
county1.text = @"CLA. COUNTY";
county1.backgroundColor = [UIColor clearColor];
county1.font = [UIFont systemFontOfSize:10];
county1.textColor = [UIColor whiteColor];
[headerView addSubview:county1];

UIImageView *caCountyImage = [[UIImageView alloc] initWithFrame:CGRectMake(107, 20, 18, 18)];
caCountyImage.image = [UIImage imageNamed:@"green.png"];
[headerView addSubview:caCountyImage];

UILabel *callType = [[UILabel alloc] initWithFrame:CGRectMake(227, 20, 75, 20)];
callType.text = @"FIRE";
callType.backgroundColor = [UIColor clearColor];
callType.font = [UIFont systemFontOfSize:10];
callType.textColor = [UIColor whiteColor];
[headerView addSubview:callType];

UIImageView *fireImage = [[UIImageView alloc] initWithFrame:CGRectMake(205, 20, 18, 18)];
fireImage.image = [UIImage imageNamed:@"red.png"];
[headerView addSubview:fireImage];

UILabel *callType1 = [[UILabel alloc] initWithFrame:CGRectMake(280, 20, 75, 20)];
callType1.text = @"EMS";
callType1.backgroundColor = [UIColor clearColor];
callType1.font = [UIFont systemFontOfSize:10];
callType1.textColor = [UIColor whiteColor];
[headerView addSubview:callType1];

UIImageView *emsImage = [[UIImageView alloc] initWithFrame:CGRectMake(258, 20, 18, 18)];
emsImage.image = [UIImage imageNamed:@"yellow.png"];
[headerView addSubview:emsImage];

return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *cellidentifier1 = @"cell1";
static NSString *cellidentifier2 = @"cell2";
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]];
self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"texture3.png"]];

if (indexPath.row % 2 == 0)
{
    UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:cellidentifier2];

    if (cell2 == nil)
    {
        cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier2];
        [cell2.contentView setAlpha:0];
        [cell2 setUserInteractionEnabled:NO];
        [cell2 setBackgroundColor:[UIColor clearColor]];
    }
    return cell2;
}

JointCAD *currentCall = [[xmlParser calls] objectAtIndex:((indexPath.row - 1 ) / 2)];

TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier1];

    if (cell == nil)
{
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier1];
}

cell.callTypeLabel.text = currentCall.currentCallType;
cell.locationLabel.text = currentCall.location;
cell.unitsLabel.text = currentCall.units;
cell.stationLabel.text = [@"Station: " stringByAppendingString:currentCall.station];

if ([currentCall.county isEqualToString:@"W"]) {

    cell.countyImageView.image = [UIImage imageNamed:@"blue.png"];
}
else {
    cell.countyImageView.image = [UIImage imageNamed:@"green.png"];
}

if ([currentCall.callType isEqualToString:@"F"]) {

    cell.callTypeImageView.image = [UIImage imageNamed:@"red.png"];
}
else {
    cell.callTypeImageView.image = [UIImage imageNamed:@"yellow.png"];
}
return cell;
}

0 个答案:

没有答案