Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIView类别-自定义方法不返回UIView_Iphone_Uitableview_Uiview_Categories - Fatal编程技术网

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

Iphone UIView类别-自定义方法不返回UIView,iphone,uitableview,uiview,categories,Iphone,Uitableview,Uiview,Categories,我使用一个方法在UIView上创建了一个类别,该方法的目的是创建并返回UIView对象。它运行时没有错误,但返回空的UIView。代码如下: #import <UIKit/UIKit.h> @interface UIView (makeTableHeader) -(UIView *) makeTableHeader:(NSString *)ImageName withTitle:(NSString *)headerTitle

我使用一个方法在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 *)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坐标——但是视图中没有图像或标签


有人看到哪里出了问题吗?

您需要将该方法设置为
方法,并按如下方式分配它:

UIView *hView = [UIView makeTableHeader:@"redGradientHeader5@2x.jpg"
             withTitle:@"Test Banner"
             usingFont:@"boldSystemFont"
           andFontSize:18];
您正在创建两个视图—一个使用alloc/init,另一个使用自定义函数。但是,您只能将第一个指定给
hView
。这是因为在
makeTableHeader
方法中,您使用
hView
创建第二个UIView,并对其应用子视图/修改,而不是修改
hView
。然后,该方法返回该视图,然后立即丢弃,因为它没有分配给任何对象

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

这是有效的:-)(我将其标记为正确答案),但我想知道:为什么您如此强烈地反对将其作为实例方法?如果方法的目的是创建并返回一个对象,那么使用类方法是否是一个普遍的经验法则?因为我见过很多创建和返回对象的实例方法。你能解释一下吗?
Factory
方法(用于创建实例的方法)应该始终是类方法,因为这样你就避免了创建一个实例只是为了创建你想要的第二个实例的开销。例外情况是,如果希望新实例部分基于旧实例。
UIView *hView = [UIView makeTableHeader:@"redGradientHeader5@2x.jpg"
             withTitle:@"Test Banner"
             usingFont:@"boldSystemFont"
           andFontSize:18];
-(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];

}