2

我在 UIView 上创建了一个 Category 方法,该方法旨在创建和返回 UIView 对象。它运行没有错误,但返回一个空的 UIView。这是代码:

#import <UIKit/UIKit.h>

@interface UIView (makeTableHeader)


 -(UIView *) makeTableHeader:(NSString *)ImageName
                  withTitle:(NSString *)headerTitle
                  usingFont:(NSString *)fontName 
                andFontSize:(CGFloat)fontSize;


@end

这是实现:

-(UIView *) makeTableHeader: (NSString *)ImageName 
              withTitle:(NSString *)headerTitle 
              usingFont:(NSString *)fontName 
            andFontSize:(CGFloat)fontSize {

     // Create a master-view:
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];

     // Create the Image:
     UIImageView *headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ImageName]];
     headerImageView.frame = CGRectMake(0, 0, 320, 34);


     // Now create the Header LABEL:
     UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 34)];
     headerLabel.text = headerTitle;
     headerLabel.font = [UIFont fontWithName:fontName size:fontSize];
     headerLabel.backgroundColor = [UIColor clearColor];
     headerLabel.textColor = [UIColor whiteColor];
     headerLabel.shadowColor = [UIColor blackColor];
     headerLabel.shadowOffset = CGSizeMake(1.0, 1.0);

     // Finally add both both Header and Label as subview to the main Header-view:
     [headerView addSubview:headerImageView];
     [headerView addSubview:headerLabel];

     return headerView;
}

现在这是我如何称呼这个类别方法:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *hView = [[UIView alloc] init];
    [hView makeTableHeader:@"redGradientHeader5@2x.jpg"
                 withTitle:@"Test Banner"
                 usingFont:@"boldSystemFont"
               andFontSize:18];

    return hView;

}

代码运行良好 - 但我得到一个空视图。有趣的是,它确实正确调整了视图的大小——给它我要求的 CGRect 坐标——但视图内没有图像或标签。

任何人都看到有什么问题吗?

4

1 回答 1

6

您需要使该方法成为class方法,并像这样分配它:

UIView *hView = [UIView makeTableHeader:@"redGradientHeader5@2x.jpg"
             withTitle:@"Test Banner"
             usingFont:@"boldSystemFont"
           andFontSize:18];

您正在制作两个视图 - 一个使用 alloc/init,一个使用您的自定义函数。但是,您只会将一个分配给hView. 这是因为在该makeTableHeader方法中,您hView用于创建第二个UIView ,并将子视图/修改应用于该 UIView,不是修改hView. 这个视图然后由方法返回,然后立即丢弃,因为它没有分配给任何东西。

或者,如果您坚持其保留为实例方法并修改视图,则只需这样做(尽管我强烈不建议这样做):

-(void) makeTableHeader: (NSString *)ImageName 
              withTitle:(NSString *)headerTitle 
              usingFont:(NSString *)fontName 
            andFontSize:(CGFloat)fontSize {
     //you may want to remove all subviews here or something
     // Create the Image:
     self.frame = CGRectMake(0, 0, 320, 34);
     UIImageView *headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ImageName]];
     headerImageView.frame = CGRectMake(0, 0, 320, 34);


     // Now create the Header LABEL:
     UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 34)];
     headerLabel.text = headerTitle;
     headerLabel.font = [UIFont fontWithName:fontName size:fontSize];
     headerLabel.backgroundColor = [UIColor clearColor];
     headerLabel.textColor = [UIColor whiteColor];
     headerLabel.shadowColor = [UIColor blackColor];
     headerLabel.shadowOffset = CGSizeMake(1.0, 1.0);

     // Finally add both both Header and Label as subview to the main Header-view:
     [self addSubview:headerImageView];
     [self addSubview:headerLabel];

}
于 2012-06-15T18:42:04.643 回答