简体   繁体   English

iOS在UITableViewHeaderFooterView.ContentView子视图中自动调整自定义单元格宽度

[英]iOS Auto-resize custom cell width in UITableViewHeaderFooterView.ContentView Subview

I create a custom cell PVIssueTypeSectionHeaderCell and add it as a subview for ContentView in the header view with this code: 我创建了一个自定义单元格PVIssueTypeSectionHeaderCell并使用以下代码将其作为ContentView的子视图添加到标题视图中:

public override UIView GetViewForHeader(UITableView tableView, nint section)
{
    var cell = (PVIssueTypeSectionHeaderCell)tableView.DequeueReusableCell(HeaderCellKey);
    if(cell == null)
    {
        cell = PVIssueTypeSectionHeaderCell.Create();
    }

    cell.ViewModel = this.GroupedIssueTypesFilter.ElementAt((int)section);

    var headerView = tableView.DequeueReusableHeaderFooterView(HeaderFooterViewKey);

    if (headerView == null)
    {
        headerView = new UITableViewHeaderFooterView(HeaderFooterViewKey);
    }

    headerView.ContentView.AddSubview(cell);

    return headerView;
}

But the custom cell's width does not auto-resize as you could see in these images below. 但是,您可以在下面的图片中看到,自定义单元格的宽度不会自动调整大小。

This is in iPad Landscape mode and in iPhone Portrait mode: 这是在iPad横向模式和iPhone纵向模式下:

ipad风景/ iphone肖像

While this one is in iPad Portrait mode: 这个是在iPad肖像模式下:

ipad肖像模式

How to make the custom cell automatically resize its width? 如何使自定义单元格自动调整其宽度?

I've finally make it work by returning my custom cell's ContentView directly in GetViewForHeader . 我终于通过直接在GetViewForHeader返回自定义单元格的ContentView来使其工作。

public override UIView GetViewForHeader(UITableView tableView, nint section)
{
    var cell = (PVIssueTypeSectionHeaderCell)tableView.DequeueReusableCell(HeaderCellKey);
    if(cell == null)
    {
        cell = PVIssueTypeSectionHeaderCell.Create();
    }

    cell.ViewModel = this.GroupedIssueTypesFilter.ElementAt((int)section);

    return cell.ContentView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止UIView动画进行子视图自动调整大小操作? - How to prevent UIView animation for subview auto-resize operations? iOS 9 上的 UITableViewHeaderFooterView 宽度问题 - UITableViewHeaderFooterView width issue on iOS 9 如果我仅包含3倍于iOS的图像,它会自动调整大小吗? - If I just include images 3 times as large for iOS, will it auto-resize? iOS表格视图具有自定义子视图的单元格自动高度 - iOS table view cell auto height with custom subview ios-如何在情节提要中自动调整视图大小? 迅捷3 - ios - How to auto-resize my view in storyboard? swift 3 将子视图添加到cell.contentView - Adding subview to cell.contentView IOS自定义表格单元格不会自动调整宽度以进行方向更改 - IOS Custom Table Cell does not Automatically Resize Width for Orientation Change IOS如何从cell.contentview.subview [0]的collectionView单元格didSelectItemAtIndexPath方法获取图像 - ios how to get image from collectionView cell didSelectItemAtIndexPath method from cell.contentview.subview[0] 在 iOS 上,向 UITableViewCell 对象“cell”和“cell.contentView”添加子视图有什么区别? - On iOS, what is the difference between adding a subview to a UITableViewCell object “cell” vs to “cell.contentView”? 自动将iPhone xib应用程序调整为ipad - Auto-resize iPhone xib app to ipad
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM