UILabel problem in viewForFooterInSection

I want to display two batches of text in two different footers in my table.

The text in section 3 uses titleForFooterInSection:

- (NSString *)tableView:(UITableView *)tv titleForFooterInSection:(NSInteger)section
{
    if (section == 3)
        return @"SOME TEXT FOR SECTION 3";
    else
        return @"";
}

and works great and is designed correctly. I also want to add more complex formatted text (just left justified), so I create a label and add it to viewForFooterInSection, which works, but does not complement (does not start with x = 20 and y = 20, starts to strongly oppose the main edge of the window) and does not look like other text.

- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
    if (section == 1) 
    {
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 100, 50)] autorelease];
        label.text = @"SOME COMPLICATED TEXT FOR SECTION 1\r\n"
        "\r\n"
        "MORE TEXT HERE\r\n"
        "AND HERE.\r\n"

        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont systemFontOfSize:14];
        label.shadowColor = [UIColor colorWithWhite:0.8 alpha:0.8];
        label.textColor = [UIColor blueColor];

        label.lineBreakMode = UILineBreakModeWordWrap;
        label.textAlignment = UITextAlignmentLeft;
        label.numberOfLines = 0;

       [label sizeToFit];

    return label;       
    }

    else
        return nil;

}

-(CGFloat)tableView:(UITableView *)tv heightForFooterInSection:(NSInteger)section
{
    if (section == 1) 
        return 180.0f;
    else if (section == 3)
        return 50.0f;
    else 
        return 0.0f;
}
+5
source share
2 answers

To get an additive, add a label to another view before returning it as a footer;

- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
  ...
  CGRect footerFrame = [tv rectForFooterInSection:1];
  CGRect labelFrame = CGRectMake(20, 20, footerFrame.size.width - 40, footerFrame.size.height - 40);

  UIView *footer = [[UIView alloc] initWithFrame:footerFrame];
  UILabel *footerLabel = [[UILabel alloc] initWithFrame:labelFrame];

  [footer addSubview:footerLabel];
  [footerLabel release];
  ...
  return footer;
}

: , *footer, autorelease.

, , , , . , , Google .

EDIT: , viewForFooterInSection, UIView;

@interface CustomHeaderView : UIView {
    NSString *headerTitle;
}
@property (nonatomic, retain) NSString *headerTitle;
@end

@implementation CustomHeaderView

@synthesize headerTitle;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {

        // create frame with padding    
        CGRect labelFrame = CGRectMake(8, 8, frame.size.width - 16, frame.size.height - 16);

        UILabel *headerText = [[UILabel alloc] initWithFrame:labelFrame];
        [headerText setNumberOfLines:0];
        [headerText setFont:[UIFont boldSystemFontOfSize:14.0]];            
        [self addSubview:headerText];

        [headerText release];           
    }
    return self;
}

- (void)setHeaderTitle:(NSString *)title {

    [title retain];
    [headerTitle release];

    UILabel *label = [[self subviews] objectAtIndex:0];
    [label setText:title];
    [label sizeToFit];

    CGRect viewFrame = [self frame];
    CGRect labelFrame = [label frame];

    viewFrame.size.height = labelFrame.size.height + 16;
    [self setFrame:viewFrame];

    [self setNeedsLayout];

    headerTitle = title;
}

- (void)dealloc {
    [headerTitle release];
    [super dealloc];
}

@end

. ;

@interface MyViewController
{
    UIView *headerView;
}
- (UIView *) headerView;
@end

@implementation MyViewController

- (UIView *) headerView {

    if(headerView)
        return headerView;

    float w = [[self view] bounds].size.width;

    // height irrelevant as long as non zero custom view will resize
    CGRect headerFrame = CGRectMake(0, 0, w, 32);

    headerView = [[CustomHeaderView alloc] initWithFrame:headerFrame];

    return headerView;
}

- (UIView *) tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)sec {
    return [self headerView];
}


- (void) viewWillAppear:(BOOL)animated {
    [(CustomHeaderView *)[self headerView] setHeaderTitle:@"My Custom Header"]];
}

@end
+11

, u - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;.

+6

All Articles