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

标签 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;

}

代码运行良好 - 但我得到的 View 是空的。有趣的是,它确实正确地调整了 View 的大小 - 为其提供了我要求的 CGRect 坐标 - 但 View 内没有图像或标签。

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

最佳答案

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

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

您正在创建两个 View - 一个使用 alloc/init,另一个使用您的自定义函数。但是,您仅将第一个分配给 hView。这是因为在 makeTableHeader 方法中,您使用 hView创建一个第二个 UIView,并应用 subview /修改那个而不是修改hView。然后该方法返回该 View ,然后立即丢弃,因为它没有分配给任何东西。

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

-(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];

}

关于iphone - UIView 类别 - 自定义方法不返回 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11056435/

相关文章:

iphone - UIView subview 中的 IOS UIImagePicker

iphone - iOS查看 'folding'动画

uitableview - 禁用滚动 tableHeaderView

ios - 允许在 TableView 行选择时执行条件操作

swift - 请告诉我如何简短地编写代码

swift - 当 UICollectionView 和 UICollectionViewCell 位于 XIB 文件中时,子视​​图为零

ios - 如何清除 iPhone/iPad 上 iOS 应用程序的数据

iphone - Interface Builder 垂直关闭

ios - swift :How can i remove the outlets from code?

iphone - 删除 View 并弹出后将 nil 分配给 UIViewController 时崩溃