【发布时间】: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