2015-04-22 197 views
1

我想重寫GetHeightForRow方法,因此當滾動到特定單元格(行)時,我的滾動工作正常。MvvmCross中的動態單元格高度

我發現這個例子here完美的工作。由於這部分代碼的:

public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) 
    { 
     if (this.offscreenCell == null) 
     { 
      this.offscreenCell = new ItemCell(); 
     } 

     var cell = this.offscreenCell; 

     cell.UpdateFonts(); 
     var item = this.model.Items[indexPath.Row]; 
     cell.Title = item.Title; 
     cell.Body = item.Body; 

     cell.SetNeedsUpdateConstraints(); 
     cell.UpdateConstraintsIfNeeded(); 

     cell.Bounds = new RectangleF(0, 0, this.TableView.Bounds.Width, this.TableView.Bounds.Height); 

     cell.SetNeedsLayout(); 
     cell.LayoutIfNeeded(); 

     var height = cell.ContentView.SystemLayoutSizeFittingSize(UIView.UILayoutFittingCompressedSize).Height; 
     height += 1; 

     return height; 
    } 

試圖把它翻譯成我MvvmCross項目,這樣的事情:

public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) 
    { 
     if (this.offscreenCell == null) 
     { 
      this.offscreenCell = new ItemCell(); 
     } 

     var cell = this.offscreenCell; 
     cell.DataContext = this.model.Items[indexPath.Row]; 

     cell.SetNeedsUpdateConstraints(); 
     cell.UpdateConstraintsIfNeeded(); 

     cell.Bounds = new RectangleF(0, 0, this.TableView.Bounds.Width, this.TableView.Bounds.Height); 

     cell.SetNeedsLayout(); 
     cell.LayoutIfNeeded(); 

     var height = cell.ContentView.SystemLayoutSizeFittingSize(UIView.UILayoutFittingCompressedSize).Height; 
     height += 1; 

     return height; 
    } 

但它總是返回36.5沒有問題的。任何線索如何使它?

感謝的問候,

回答

0

你可能有更好的運氣與UICollectionView,而不是一個UITableView。總的來說,它們比表格視圖更靈活。

如果您嘗試一下,您會發現爲單個項目(集合視圖具有項目而不是單元格)自定義高度很容易。

首先,你需要創建一個自定義UICollectionViewDelegateFlowLayout:

public class ThingDelegateFlowLayout : UICollectionViewDelegateFlowLayout 
{ 
    List<Thing> Things; 

    public ThingDelegateFlowLayout(List<Thing> things) 
    { 
     Things = things; 
    } 

    // this is the override that allows you to dynamically size UICollectionView items 
    public override SizeF GetSizeForItem (UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath) 
    { 
     return new SizeF(
      width: UIScreen.MainScreen.Bounds.Width, // Set the width to whatever you want. In this case, it's the screen width. 
      height: Things[indexPath.Row].Height // Set the item height 
     ); 
    } 
} 

然後在您的視圖控制器做類似以下內容。需要注意的重要線...

thingCollectionView.Delegate = new ThingDelegateFlowLayout(Things);

...在底部附近,在那裏你指派自定義UICollectionViewDelegateFlowLayout到UICollectionView:

public class ThingViewController : UIViewController 
{ 
    private List<Thing> Things; 

    public ThingViewController() 
    { 
     Things = GetThings(); 
    } 

    // gets a list of sweet hair dos 
    private List<Thing> GetThings() 
    { 
     // do something here to get a sweet list of hair dos 
    } 

    public override void ViewDidLoad() 
    { 
     UICollectionViewFlowLayout layout = new UICollectionViewFlowLayout() 
     { 
      HeaderReferenceSize = new SizeF(0, 0), // no header 
      FooterReferenceSize = new SizeF(0, 0), // no footer 
      SectionInset = new UIEdgeInsets(0, 0, 0, 0), // no section inset 
      ItemSize = new SizeF(UIScreen.MainScreen.Bounds.Width, 100f), // set each item to the full width of the screen and 100 pixels tall 
      ScrollDirection = UICollectionViewScrollDirection.Vertical, // set the collection to scroll vertically 
      MinimumInteritemSpacing = 0, // no item spacing 
      MinimumLineSpacing = 0, // no line spacing 
     }; 

     var thingCollectionViewController = new UICollectionViewController(layout); 
     var thingCollectionView = new UICollectionView(UIScreen.MainScreen.Bounds.Size); 

     thingCollectionViewController.CollectionView.Frame = thingCollectionView.Frame; 
     thingCollectionView = thingCollectionViewController.CollectionView; 
     thingCollectionView.Delegate = new ThingDelegateFlowLayout(Things); 
     thingCollectionView.BackgroundColor = UIColor.Clear; // or whatever 
     thingCollectionView.BackgroundView = new UIView() 
     { 
      BackgroundColor = UIColor.Clear // or whatever 
     }; 

     View.AddSubview (thingCollectionView); 
    } 
} 

我不知道究竟如何用UITableView來做到這一點。我基本上只是停止使用表視圖,因爲我更喜歡UICollectionViews。

+0

它會導致我面臨同樣的問題。如果我想要一個動態高度視圖控制器單元格,我將不得不計算它在thingdelegateflowlayout.getsizeforitem ...問題是如何計算它與mvxtableviewcell ... –

相關問題