我在 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 坐标——但视图内没有图像或标签。
任何人都看到有什么问题吗?