您的位置:首页 > 其它

解决TalbleView头部或底部子控件不显示问题

2015-09-21 22:31 134 查看
在自定义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(0, 10, 0, 0);
nameView.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[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 = 0;
CGFloat countH = self.frame.size.height;
CGFloat countW = 150;
CGFloat countX = self.frame.size.width - 10 - 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: