【问题标题】:Trying to create a Netflix like UI - Project included尝试创建类似 Netflix 的 UI - 包含项目
【发布时间】:2016-11-04 12:02:05
【问题描述】:

大家晚上好。

案例研究: 目前我正在开发一个 tvos 应用程序,它需要像 Netflix 这样的垂直滚动。我发现以下文章解释了这种方法:https://www.thorntech.com/2015/08/want-your-swift-app-to-scroll-in-two-directions-like-netflix-heres-how/

问题: 我的类(“DashboardCollectionView.cs”)的构造函数没有被调用,所以我的单元格没有被初始化。结果我收到一个没有任何 collectionviewcells 的视图,如下图所示:

有关代码和项目的信息:

我已将解决方案附加为 zip 文件。我希望有一个人可以帮助我。我真的是 .ios 的新手,所以也许这很容易。

http://www35.zippyshare.com/v/cTJje8WL/file.html

编辑: 部分代码

public partial class DashboardCollectionView : UICollectionView
{
    public DashboardCollectionView (IntPtr handle) : base (handle)
    {
        RegisterClassForCell(typeof(DashboardCollectionViewCell), "movieCell");
        DataSource = new DashboardCollectionViewDataSource();
        Delegate = new DashboardCollectionViewDelegateFlowLayout();
    }
}

public partial class DashboardCollectionViewCell : UICollectionViewCell
{
    public DashboardCollectionViewCell (IntPtr handle) : base (handle)
    {
    }
}

public class DashboardCollectionViewDataSource: UIKit.UICollectionViewDataSource
{

        public DashboardCollectionViewDataSource()
        {
        }

        public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var movieCell = (DashboardCollectionViewCell)collectionView.DequeueReusableCell("movieCell", indexPath);
            return movieCell;
        }

        public override nint GetItemsCount(UICollectionView collectionView, nint section)
        {
            return 12;
        }

        public override nint NumberOfSections(UICollectionView collectionView)
        {
            return 1;
        }
    }

public class DashboardCollectionViewDelegateFlowLayout : UIKit.UICollectionViewDelegateFlowLayout
{
    public DashboardCollectionViewDelegateFlowLayout()
    {
    }

    public override CoreGraphics.CGSize GetSizeForItem(UIKit.UICollectionView collectionView, UIKit.UICollectionViewLayout layout, Foundation.NSIndexPath indexPath)
    {
        var itemsPerRow = 4;
        var hardCodedPadding = 10;
        var itemWidth = (collectionView.Bounds.Width / itemsPerRow) - hardCodedPadding;
        var itemHeight = collectionView.Bounds.Height - (2 * hardCodedPadding);
        return new CoreGraphics.CGSize(itemWidth, itemHeight);
    }
}

 public partial class DashboardTableViewController : UITableViewController
{
    private String[] categories = new String[] { "Kürzlich hinzugefügt", "Beliebt" };
    private String cardCellId = "cell";
    public DashboardTableViewController(IntPtr handle) : base(handle)
    {

    }



    public override nint NumberOfSections(UITableView tableView)
    {
        return categories.Length;
    }

    public override string TitleForHeader(UITableView tableView, nint section)
    {
        return categories[section];
    }

    public override nint RowsInSection(UITableView tableView, nint section)
    {
        return 1;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = tableView.DequeueReusableCell(cardCellId, indexPath);
        return cell;
    }
}

【问题讨论】:

  • 在此处包含相关代码,而不是附加随机文件,该文件可能被感染并包含恶意代码。 SO 问题最好是自包含的,即使在您从共享主机中删除该文件后,它们也可以访问。
  • @Cheesebaron 我已经添加了文件的代码。
  • 在 GetCell 中,您永远不会创建任何新单元格。
  • @Cheesebaron 是的,没错,我在故事板上定义了单元格。我不确定这是否可行,但在另一个项目中我做了类似的事情并且它正在工作。

标签: uitableview xamarin xamarin.ios uicollectionview tvos


【解决方案1】:

问题出在这部分

3.现在是棘手的部分——将 Collection View 的 DataSource 和 Delegate 连接到单元格。如果您从 Collection View 的 Connections Inspector 拖动到视图层次结构中的 Table View Cell 会更容易。

我不确定如何使用 VS 故事板来做到这一点,您可能需要使用 XCode。您尝试以编程方式执行此操作,但这不起作用。

因此,如果不能使用 XCode,您可以欺骗这部分并为您的 DashboardCollectionView 命名以强制创建它。但是如果不创建“表格视图单元”的自定义类,您就无法做到这一点。 你还需要注释掉

//RegisterClassForCell(typeof(DashboardCollectionViewCell), "movieCell");

完成这 3 个步骤后,您应该能够看到您的单元格。我在 iOS 9 模拟器上运行它,但无法垂直或水平滚动,但这是一个不同的问题。

【讨论】:

  • 是的,谢谢您的回答!经过数小时的研究,我找不到滚动水平部分的解决方案。我决定在 tvos 上为这种机制采取另一种方法。使用以下方法最终达到了预期的效果:brianjcoleman.com/tvos-tutorial-video-app-in-swift
猜你喜欢
  • 2015-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 2019-06-07
  • 2011-01-29
  • 1970-01-01
  • 2020-03-29
相关资源
最近更新 更多