尝试创建类似 UI 的 Netflix - 包含项目
Trying to create a Netflix like UI - Project included
大家晚上好
案例研究:
目前我正在开发一个 tvos 应用程序,它需要像 Netflix 一样的垂直滚动。我发现以下文章解释了该方法:https://www.thorntech.com/2015/08/want-your-swift-app-to-scroll-in-two-directions-like-netflix-heres-how/
问题:
我的 class ("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;
}
}
问题出在这部分
3。现在是棘手的部分——将 Collection View 的 DataSource 和 Delegate 连接到单元格。如果您从集合视图的连接检查器拖动到视图层次结构中的 Table 视图单元格,会更容易。
我不确定如何使用 VS 故事板来做到这一点,您可能需要使用 XCode。您有点尝试以编程方式执行此操作,但这不起作用。
因此,如果使用 XCode 不是一个选项,您可以欺骗这部分并为您的 DashboardCollectionView 指定一个名称以强制创建它。但是如果不为您的 "Table View Cell" 创建自定义 class,您将无法做到这一点。
你还需要注释掉
//RegisterClassForCell(typeof(DashboardCollectionViewCell), "movieCell");
完成这 3 个步骤后,您应该能够看到您的细胞。我 运行 它在 iOS 9 模拟器上,但无法获得任何垂直或水平滚动,但这是一个不同的问题。
大家晚上好
案例研究: 目前我正在开发一个 tvos 应用程序,它需要像 Netflix 一样的垂直滚动。我发现以下文章解释了该方法:https://www.thorntech.com/2015/08/want-your-swift-app-to-scroll-in-two-directions-like-netflix-heres-how/
问题: 我的 class ("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;
}
}
问题出在这部分
3。现在是棘手的部分——将 Collection View 的 DataSource 和 Delegate 连接到单元格。如果您从集合视图的连接检查器拖动到视图层次结构中的 Table 视图单元格,会更容易。
我不确定如何使用 VS 故事板来做到这一点,您可能需要使用 XCode。您有点尝试以编程方式执行此操作,但这不起作用。
因此,如果使用 XCode 不是一个选项,您可以欺骗这部分并为您的 DashboardCollectionView 指定一个名称以强制创建它。但是如果不为您的 "Table View Cell" 创建自定义 class,您将无法做到这一点。 你还需要注释掉
//RegisterClassForCell(typeof(DashboardCollectionViewCell), "movieCell");
完成这 3 个步骤后,您应该能够看到您的细胞。我 运行 它在 iOS 9 模拟器上,但无法获得任何垂直或水平滚动,但这是一个不同的问题。