0

我正在尝试使用 Xamarin.iOS。在我开始自定义 UI 之前,一切都是有意义的。您将如何为集合控件及其单元格控件创建具有自定义外观和感觉的集合?

4

2 回答 2

4

UICollectionView 有点类似于 UITableView。要获得您想要的自定义,您首先需要定义您的自定义单元类:

public class AnimalCell : UICollectionViewCell
{
        UIImageView imageView;

        [Export ("initWithFrame:")]
        public AnimalCell (System.Drawing.RectangleF frame) : base (frame)
        {
                BackgroundView = new UIView{BackgroundColor = UIColor.Orange};

                SelectedBackgroundView = new UIView{BackgroundColor = UIColor.Green};

                ContentView.Layer.BorderColor = UIColor.LightGray.CGColor;
                ContentView.Layer.BorderWidth = 2.0f;
                ContentView.BackgroundColor = UIColor.White;
                ContentView.Transform = CGAffineTransform.MakeScale (0.8f, 0.8f);

                imageView = new UIImageView (UIImage.FromBundle ("placeholder.png"));
                imageView.Center = ContentView.Center;
                imageView.Transform = CGAffineTransform.MakeScale (0.7f, 0.7f);

                ContentView.AddSubview (imageView);
        }

        public UIImage Image {
                set {
                        imageView.Image = value;
                }
        }
}

然后,您可以通过覆盖 GetCell 方法在 UICollectionViewDataSource 类中引用此单元格:

public override UICollectionViewCell GetCell (UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
{
        var animalCell = (AnimalCell)collectionView.DequeueReusableCell (animalCellId, indexPath);

        var animal = animals [indexPath.Row];

        animalCell.Image = animal.Image;

        return animalCell;
}

有关更多信息,您应该在 Xamarin 的网站上查看本教程,我从中提取了这些示例: http: //docs.xamarin.com/guides/ios/user_interface/introduction_to_collection_views

于 2013-05-22T19:22:10.807 回答
0

要添加背景图片,试试这个(在 ViewDidLoad 中)

myview.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("myimage.png"));
于 2013-05-22T16:16:56.763 回答