Xamarin中的iOS自定义集合视图单元格

时间:2013-05-22 15:52:41

标签: ios xamarin

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

myview.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("myimage.png"));