我正在尝试以编程方式在自定义UITableViewHeaderFooterView
类上添加按钮,如下所示,它不会出现。
我想知道我错过了什么或做错了什么?
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
UIButton * clickBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.bounds.size.width-50, 0, 50, 50)];
clickBtn.imageView.image = [UIImage imageNamed:@"carat-open.png"];
[self.contentView addSubview:clickBtn];
}
return self;
}
答案 0 :(得分:1)
在此函数内创建UIButton
,并将按钮作为子视图添加到标题视图
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton * clickBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.bounds.size.width-50, 0, 50, 50)];
clickBtn.imageView.image = [UIImage imageNamed:@"carat-open.png"];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:clickBtn];
headerView.backgroundColor=[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
return headerView;
}
答案 1 :(得分:0)
在“页脚”中添加自定义视图:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [self getViewForFooterForSection];
}
- (UIView*)getViewForFooterForSection
{
UIView *sectionFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
sectionFooterView.backgroundColor = [UIColor redColor];
return sectionFooterView;
}
从自定义视图文件
添加节页脚的自定义视图
CustomFooter
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.frame = frame;
self.backgroundColor = [UIColor blueColor];
}
return self;
}
查看控制器
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [self getViewForFooterForSection];
}
- (UIView*)getViewForFooterForSection
{
CustomFooter *sectionFooterView = [[CustomFooter alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
return sectionFooterView;
}
在表格页脚视图中添加自定义视图
tbl.tableFooterView = [self getViewForTableView];
- (UIView*)getViewForTableView
{
UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
tableFooterView.backgroundColor = [UIColor greenColor];
return tableFooterView;
}
答案 2 :(得分:0)
你不能设置那样的按钮图像。
clickBtn.imageView.image = [UIImage imageNamed:@“carat-open.png”];
imageView属性是readonly。
使用这个