【发布时间】:2016-04-19 21:52:11
【问题描述】:
我正在开发 Xamarin IOS,我想使用集合视图概念以网格样式显示项目。 我尝试了示例收集视图示例。 Sample Collection View Example
但项目显示为给定的示例。但我想以网格样式显示项目,请看下图。
【问题讨论】:
-
贴一些代码,否则没人知道发生了什么
标签: c# xamarin xamarin.ios
我正在开发 Xamarin IOS,我想使用集合视图概念以网格样式显示项目。 我尝试了示例收集视图示例。 Sample Collection View Example
但项目显示为给定的示例。但我想以网格样式显示项目,请看下图。
【问题讨论】:
标签: c# xamarin xamarin.ios
这是我在同一示例代码上进行的快速测试中的一个相当粗略的示例:
[Export ("initWithFrame:")]
public ProductCell (CGRect frame) : base (frame)
{
BackgroundView = new UIView{ BackgroundColor = UIColor.DarkGray };
SelectedBackgroundView = new UIView{ BackgroundColor = UIColor.Green };
ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
ContentView.Layer.BorderWidth = 2.0f;
ContentView.BackgroundColor = UIColor.Gray;
cellViewContainer = new UIView ();
cellViewContainer.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
imageView = new UIImageView (UIImage.FromBundle ("placeholder.png"));
productName = new UILabel {
Text = "Name",
TextColor = UIColor.Black,
BackgroundColor = UIColor.White,
TextAlignment = UITextAlignment.Center
};
price = new UILabel {
Text = "Price",
TextColor = UIColor.Black,
BackgroundColor = UIColor.Red,
TextAlignment = UITextAlignment.Center
};
var labelHeight = (ContentView.Bounds.Height - 2) / 2;
var labelWidth = (ContentView.Bounds.Width - 2 ) / 2;
productName.Frame = new CGRect(5, 5, labelWidth, labelHeight);
price.Frame = new CGRect(5, labelHeight, labelWidth, labelHeight);
imageView.Frame = new CGRect (labelWidth, 0, labelWidth, ContentView.Bounds.Height - 2);
ContentView.AddSubviews (new UIView[] { imageView, productName, price });
}
【讨论】:
在那个示例代码中你能不能这样做:
// Flow Layout
flowLayout = new UICollectionViewFlowLayout (){
ItemSize = new CGSize ((float)UIScreen.MainScreen.Bounds.Size.Width/2.0f, (float)UIScreen.MainScreen.Bounds.Size.Width/2.0f),
HeaderReferenceSize = new CGSize (100, 100),
SectionInset = new UIEdgeInsets (0,0,0,0),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
MinimumInteritemSpacing = 0, // minimum spacing between cells
MinimumLineSpacing = 0 // minimum spacing between rows if ScrollDirection is Vertical or between columns if Horizontal
};
使单元格大小为屏幕宽度的一半。如果您想要该模型中的其他元素,则需要发布一些代码。
This 也是一个很好的关于 UICollectionViews 的教程,它在本机代码中自定义布局,但应该很容易翻译成 c#
或者看看这个问题UICollectionView for Grid Layout with Xamarin.iOS (MonoTouch)?
【讨论】: