单触式对话框-多行RootElement



我正在使用MonoTouch.Dialog在我的Xamarin iOS应用程序中创建一个页面。

我正试图利用GetCell方法创建一个多行RootElement。这在加载时可以很好地工作,但如果您单击另一个选项卡并返回,则元素会收缩回默认大小(同样,当您单击元素时,您会看到它在转换之前收缩)。

我曾经尝试过扰乱UnevenRows,但到目前为止没有成功。

public partial class TestController : UITabBarController
{
    public TestController()
        : base("TestController", null)
    {
    }
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        var navController = new UINavigationController
        {
            Title = "Test1"
        };
        navController.PushViewController(new TestDialogViewController(), false);
        ViewControllers = new[]
        {
            navController, 
            new UIViewController
            {
                Title = "Test2"
            },
        };
    }
}
public class TestDialogViewController : DialogViewController
{
    public TestDialogViewController() : base(new RootElement("Test"))
    {
        Root.UnevenRows = true; // has no effect
        var testSection = new Section("Test section");
        var testChildRootElement = new CustomRootElement("MultinLinenElement")
        {
            UnevenRows = true // has no effect
        };
        var testChildSection = new Section("Test child section");
        var testEntryElement = new EntryElement(string.Empty, string.Empty, "Test entry element");
        testChildSection.Add(testEntryElement);
        testChildRootElement.Add(testChildSection);
        testSection.Add(testChildRootElement);
        Root.Add(testSection);
    }
}
public class CustomRootElement : RootElement
{
    public CustomRootElement(string caption) : base(caption) {}
    public override UITableViewCell GetCell(UITableView tv)
    {
        var cell = base.GetCell(tv);
        // Setup Multi-line Element
        cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
        cell.TextLabel.Lines = 0;
        return cell;
    }
}

找到了一个似乎能解决问题的解决方法

将其添加到DialogViewController:

public override void ViewWillLayoutSubviews()
    {
        if (TableView != null && TableView.VisibleCells.Any())
        {
            foreach (var cell in TableView.VisibleCells)
            {
                cell.SizeToFit();
            }
        }
        base.ViewWillLayoutSubviews();
    }

更新:

上述解决方案不适用于多个元素,因为绘制表格时没有计算高度。

一个更好的解决方案是使用自定义UITableViewSource(继承自MonoTouch.Dialog.DialogViewController.SizeingSource,默认情况下使用它具有所有额外功能)。

为了清楚起见,下面是一个基本实现,但您可能不希望每次在生产版本中调用GetHeight()时都调用GetCell()。

public partial class TestController : UITabBarController
{
    public TestController() : base("TestController", null) {}
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        var navController = new UINavigationController();
        navController.PushViewController(new TestDialogViewController(), false);
        navController.TopViewController.Title = "Tab 1";
        ViewControllers = new[]
        {
            navController, 
            new UIViewController { Title = "Test2" }
        };
    }
}
public class TestDialogViewController : DialogViewController
{
    public TestDialogViewController() : base(new RootElement("Test"))
    {
        Root.Add(new Section("Test section")
        {
            new CustomRootElement("MultinLinenElement")
            {
                new Section("Test child section")
                {
                    new EntryElement("Test element", string.Empty, "value")
                },
            },
            new EntryElement("Test element", string.Empty, "value")
        });
    }
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        TableView.Source = new CustomTableViewSource(this);
    }
}
public class CustomTableViewSource : DialogViewController.SizingSource
{
    public CustomTableViewSource(DialogViewController controller) : base(controller) {}
    public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
        // Recommend storing these values, as is appropriate for your usage
        var cell = GetCell(tableView, indexPath);
        cell.SizeToFit();
        return cell.Frame.Height;
    }
}

public class CustomRootElement : RootElement
{
    public CustomRootElement(string caption) : base(caption) {}
    public override UITableViewCell GetCell(UITableView tv)
    {
        var cell = base.GetCell(tv);
        // Setup Multi-line Element
        cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
        cell.TextLabel.Lines = 0;
        return cell;
    }
}

最新更新