ios - 自定义 View 不适合 UItableview 中的标题

标签 ios objective-c uitableview custom-view uitableviewsectionheader

您好,我尝试在 TableView 标题上设置自定义 View ,但自定义 View 不适合标题。

自定义 View 如下图所示,在此图像中自定义 View 是橙色,标题 View 是灰色。但我希望适合自定义 View 充满标题 View 。

enter image description here

请帮忙。

我的代码:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {

        UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, _TableList.frame.size.width, 80)];
        header = [[HeaderView alloc] initWithFrame:CGRectMake(sectionView.frame.origin.x, sectionView.frame.origin.y, sectionView.frame.size.width, sectionView.frame.size.height)];
        [sectionView addSubview:header];
        sectionView.tag=section;
        sectionView.backgroundColor = [UIColor grayColor];

        UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
        [sectionView addGestureRecognizer:headerTapped];

        return  sectionView;
    }

最佳答案

如果您使用 Xib 将自定义 View 加载为节标题,则执行如下代码:

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderView *headerview = [[[NSBundle mainBundle] loadNibNamed:@"SecHeader"
                                                            owner:self
                                                          options:nil] objectAtIndex:0];
    headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 80);

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [headerview addGestureRecognizer:headerTapped];

    return headerview;
}

关于ios - 自定义 View 不适合 UItableview 中的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38496274/

相关文章:

ios - 依赖项 `Mixpanel` 未在任何具体目标中使用

ios - Flurry 在 iPhone 5 上启动需要 4 秒

ios - 如何在 xCode 5 上安装 iOS 4.3 模拟器?

ios - 布局管理器 boundingRectForGlyphRange :inTextContainer: does not work for all strings

ios - 使用 iOS 应用程序的 Objective-C 创建 MS Excel 类型的电子表格

ios - 在 tableView 中显示数组

ios - TableView AllowsMultipleSelection - 仍然允许多项选择

ios - AVAudioPlayer 噼啪声和爆裂声

ios - 在 32 位和 64 位架构上格式化 NS(U)Integer 时类型转换的替代方法?

ios - 如何在 tableView 中选择单元格时触发 pickerview