0

我使用MonoTouch.Dialog在Xamarin iOS应用程序中创建页面。Monotouch对话框 - 多行RootElement

我想通过利用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("Multi\nLine\nElement") 
     { 
      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; 
    } 
} 

回答

1

研究发现,似乎这样的伎俩

一下添加到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.SizingSource这是默认使用的继承了这些额外的功能)。

为了清楚起见,下面是一个基本实现,但是您可能不希望每次在生产版本中调用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("Multi\nLine\nElement") 
      { 
       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; 
    } 
}