簡體   English   中英

iOS在UITableViewHeaderFooterView.ContentView子視圖中自動調整自定義單元格寬度

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

我創建了一個自定義單元格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;
}

但是,您可以在下面的圖片中看到,自定義單元格的寬度不會自動調整大小。

這是在iPad橫向模式和iPhone縱向模式下:

ipad風景/ iphone肖像

這個是在iPad肖像模式下:

ipad肖像模式

如何使自定義單元格自動調整其寬度?

我終於通過直接在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM