简体   繁体   中英

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:

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肖像

While this one is in iPad Portrait mode:

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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM