在自定义cell头部控件UITableViewHeaderFooterView(和自定义cell的方法几乎一样)时,出现了头部控件子控件不显示的问题。
注意和自定义cell的区别。
.h文件
1 #import <UIKit/UIKit.h> 2 #import "CHModleGroup.h" 3 @interface HeaderView : UITableViewHeaderFooterView 4 @property (nonatomic, weak) UILabel *count; 5 @property (nonatomic, weak) UIButton *name; 6 + (instancetype)headerViewWithTableView:(UITableView *)tableView; 7 8 @property (nonatomic, strong) CHModleGroup *group; 9 10 @end
.m文件
1 #import "HeaderView.h" 2 3 @implementation HeaderView 4 + (instancetype)headerViewWithTableView:(UITableView *)tableView{ 5 static NSString *ID = @"header"; 6 HeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID]; 7 if (header == nil) { 8 header = [[HeaderView alloc]initWithReuseIdentifier:ID]; 9 } 10 return header; 11 } 12 13 - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier 14 { 15 if (self = [super initWithReuseIdentifier:reuseIdentifier]) { 16 UIButton *nameView = [UIButton buttonWithType:UIButtonTypeCustom]; 17 // 背景图片 18 [nameView setBackgroundImage:[UIImage imageNamed:@"发布新闻背景副本"] forState:UIControlStateNormal]; 19 [nameView setBackgroundImage:[UIImage imageNamed:@"welcome3"] forState:UIControlStateHighlighted]; 20 // 设置按钮内部的左边箭头图片 21 [nameView setImage:[UIImage imageNamed:@"buddy_header_arrow副本"] forState:UIControlStateNormal]; 22 [nameView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 23 // 设置按钮的内容左对齐 24 nameView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; 25 // 设置按钮的内边距 26 // nameView.imageEdgeInsets 27 nameView.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); 28 nameView.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); 29 [self.contentView addSubview:nameView]; 30 self.nameView = nameView; 31 32 33 34 // 2.添加好友数 35 UILabel *countView = [[UILabel alloc] init]; 36 countView.textAlignment = NSTextAlignmentRight; 37 countView.textColor = [UIColor grayColor]; 38 [self.contentView addSubview:countView]; 39 self.countView = countView; 40 41 42 } 43 return self; 44 } 45 46 /** 47 * 当一个控件的frame发生改变的时候就会调用 48 * 49 * 一般在这里布局内部的子控件(设置子控件的frame) 50 */ 51 - (void)layoutSubviews 52 { 53 //一定要调用super的方法 54 [super layoutSubviews]; 55 56 // 1.设置按钮的frame 57 self.nameView.frame = self.bounds; 58 59 // 2.设置数的frame 60 CGFloat countY = 0; 61 CGFloat countH = self.frame.size.height; 62 CGFloat countW = 150; 63 CGFloat countX = self.frame.size.width - 10 - countW; 64 self.countView.frame = CGRectMake(countX, countY, countW, countH); 65 } 66 67 //设置数据 68 - (void)setGroup:(MJFriendGroup *)group 69 { 70 _group = group; 71 72 // 1.设置按钮文字(组名) 73 [self.name setTitle:group.name forState:UIControlStateNormal]; 74 75 // 2.设置数量(总数) 76 self.count.text = [NSString stringWithFormat:@"%lu", friends.count]; 77 } 78 79 80 @end
时间: 2024-08-06 11:57:13