UIView类别 - 不返回UIView的自定义方法

时间:2012-06-15 18:40:40

标签: iphone uitableview uiview categories

我在UIView上创建了一个类别,其方法是创建和返回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坐标 - 但视图中没有图像或标签。

有人看到了什么问题吗?

1 个答案:

答案 0 :(得分:6)

您需要将方法设为class方法,并将其指定为:

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

您正在制作两个视图 - 一个使用alloc / init,另一个使用自定义函数。但是,您只需将第一个分配给hView。这是因为在makeTableHeader方法中,您使用hView创建 第二个 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];

}