
正文
解决TalbleView头部或底部子控件不显示问题
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
在自定义cell头部控件UITableViewHeaderFooterView(和自定义cell的方法几乎一样)时,出现了头部控件子控件不显示的问题。
注意和自定义cell的区别。
.h文件
#import <UIKit/UIKit.h>
#import "CHModleGroup.h"
@interface HeaderView : UITableViewHeaderFooterView
@property (nonatomic, weak) UILabel *count;
@property (nonatomic, weak) UIButton *name;
+ (instancetype)headerViewWithTableView:(UITableView *)tableView; @property (nonatomic, strong) CHModleGroup *group; @end
.m文件
#import "HeaderView.h" @implementation HeaderView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView{
static NSString *ID = @"header";
HeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (header == nil) {
header = [[HeaderView alloc]initWithReuseIdentifier:ID];
}
return header;
} - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
UIButton *nameView = [UIButton buttonWithType:UIButtonTypeCustom];
// 背景图片
[nameView setBackgroundImage:[UIImage imageNamed:@"发布新闻背景副本"] forState:UIControlStateNormal];
[nameView setBackgroundImage:[UIImage imageNamed:@"welcome3"] forState:UIControlStateHighlighted];
// 设置按钮内部的左边箭头图片
[nameView setImage:[UIImage imageNamed:@"buddy_header_arrow副本"] forState:UIControlStateNormal];
[nameView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 设置按钮的内容左对齐
nameView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 设置按钮的内边距
// nameView.imageEdgeInsets
nameView.titleEdgeInsets = UIEdgeInsetsMake(, , , );
nameView.contentEdgeInsets = UIEdgeInsetsMake(, , , );
[self.contentView addSubview:nameView];
self.nameView = nameView; // 2.添加好友数
UILabel *countView = [[UILabel alloc] init];
countView.textAlignment = NSTextAlignmentRight;
countView.textColor = [UIColor grayColor];
[self.contentView addSubview:countView];
self.countView = countView; }
return self;
} /**
* 当一个控件的frame发生改变的时候就会调用
*
* 一般在这里布局内部的子控件(设置子控件的frame)
*/
- (void)layoutSubviews
{
//一定要调用super的方法
[super layoutSubviews]; // 1.设置按钮的frame
self.nameView.frame = self.bounds; // 2.设置数的frame
CGFloat countY = ;
CGFloat countH = self.frame.size.height;
CGFloat countW = ;
CGFloat countX = self.frame.size.width - - countW;
self.countView.frame = CGRectMake(countX, countY, countW, countH);
} //设置数据
- (void)setGroup:(MJFriendGroup *)group
{
_group = group; // 1.设置按钮文字(组名)
[self.name setTitle:group.name forState:UIControlStateNormal]; // 2.设置数量(总数)
self.count.text = [NSString stringWithFormat:@"%lu", friends.count];
} @end








