MvvmCross中的动态细胞高度

时间:2015-04-22 23:24:14

标签: ios dynamic xamarin height mvvmcross

我想覆盖GetHeightForRow方法,因此滚动到特定单元格(行)时滚动效果非常好。

我发现这个例子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无所谓。有关如何制作它的任何线索?

谢谢你,

1 个答案:

答案 0 :(得分: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。