Pregunta

I am trying to add a custom view to the header of each of my UITableView's sections. I am using this delegate method to return the desired view. It is working partly as it causes the cell sections to be spread out as if there was a header there, however neither the text or UIButton actually appear. I know this method is being called as I places an NSLog in the method to see if it was. Am I making some kind of silly mistake, or is this not the right way of doing this?

 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView* customView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)];
        customView.backgroundColor = [UIColor clearColor];

        UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44.0)];
        headerLabel.textColor = [UIColor darkGrayColor];
        headerLabel.font = [UIFont boldSystemFontOfSize:16];


        UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        // add button to right corner of section

        return customView;
        switch (section) {
            case 0:
                headerLabel.text = @"Team Name";
                break;
            case 1:
                headerLabel.text = @"Captain";
                break;
            case 2:
                headerLabel.text = @"Wicket Keeper";
                break;
            case 3:
                headerLabel.text = @"Batting Order";
                headerButton.center = CGPointMake( 160.0, 22.0);
                headerButton.backgroundColor = [UIColor blueColor];
                headerButton.tag = section;
                [headerButton   addTarget:self action:@selector(enableCellReordering:) forControlEvents:UIControlEventTouchUpInside];
                [customView addSubview:headerButton];
                break;
            default:
                break;
        }

        [customView addSubview:headerLabel];
        return customView;
    }
¿Fue útil?

Solución 2

You return your customview twice , one before switch statement one after switch statement twice just need to remove return customView; before switch (section)

Otros consejos

Found it, you return customView; before the switch statement.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
    /* Create custom view to display section header... */
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
     NSString *string =[list objectAtIndex:section];
    /* Section header is in 0th index... */
    [label setText:string];
    [view addSubview:label];
    [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
    return view;
}

What about the custom header origin.x and origin.y ? when I set them non-zero, but it's still next to the ledt edge.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top