Элемент CollectionView Выбранный метод не срабатывает в iOS - PullRequest
0 голосов
/ 10 мая 2018

Я работаю над CollectionView, мой CollectionView работает нормально, но когда я выбираю элемент, нажав на ячейку. ItemSelected не огонь. Я пытаюсь найти что-то на Google и SO, но мне ничего не помогает.

ViewController:

public partial class SearchMarketViewController : UIViewController
    {
        UIView shapeContainer;
        UICollectionView shapeCollectionView;
        UICollectionViewShapeSource shapeSource;
        UICollectionViewFlowLayout shapeLayout;

        public SearchMarketViewController (IntPtr handle) : base (handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            scrollView.ScrollEnabled = false;

            this.Title = "Search";

            AasthaFontAndSize.M14PrimaryThemeLabel(lbl_size);
            AasthaFontAndSize.M14PrimaryThemeLabel(lbl_shape);

            AasthaFontAndSize.M14PrimaryThemeLabel(lbl_color);

            shapeList = new List<string>();
            shapeList.Add("Round");
            shapeList.Add("Pear");
            shapeList.Add("Marquise");
            shapeList.Add("Oval");
            shapeList.Add("Heart");
            shapeList.Add("Princess");
            shapeList.Add("Emerald");
            shapeList.Add("Cushion");
            shapeList.Add("Radiant");
            shapeList.Add("Asscher");
            shapeList.Add("CushionBr");
            shapeList.Add("Triangle");
            shapeList.Add("Chakari");

            shapeLayout = new UICollectionViewFlowLayout
            {
                SectionInset = new UIEdgeInsets(0, 0, 0, 0),
                MinimumInteritemSpacing = 0,
                MinimumLineSpacing = 0,
                ItemSize = new SizeF(80, 110),
                ScrollDirection = UICollectionViewScrollDirection.Horizontal
            };

            shapeContainer = new UIView();
            shapeContainer.Frame = new CoreGraphics.CGRect(15.0f, 95.0f, UIScreen.MainScreen.Bounds.Size.Width - 30.0f, 110.0f);
            shapeContainer.BackgroundColor = UIColor.White;
            contentView.AddSubview(shapeContainer);

            shapeCollectionView = new UICollectionView(shapeContainer.Bounds, shapeLayout);
            shapeCollectionView.ContentSize = new CoreGraphics.CGSize(shapeContainer.Frame.Size.Width, shapeContainer.Frame.Size.Height);
            shapeCollectionView.CollectionViewLayout = shapeLayout;
            shapeCollectionView.BackgroundColor = UIColor.White;
            shapeCollectionView.ShowsHorizontalScrollIndicator = false;
            shapeCollectionView.AllowsSelection = true;

            shapeSource = new UICollectionViewShapeSource(shapeList);

            shapeCollectionView.RegisterClassForCell(typeof(ShapeCell), ShapeCell.CellId);
            shapeCollectionView.Source = shapeSource;

            shapeContainer.AddSubview(shapeCollectionView);

    }

ShapeCell.cs

class ShapeCell : UICollectionViewCell
        {
            UILabel label;
            UIImageView imageView;

            public static readonly NSString CellId = new NSString("ShapeCell");

            public string NameText
            {
                get{
                    return label.Text;
                }
                set{
                    label.Text = value;
                    label.TextColor = UIColor.Black;
                    SetNeedsDisplay();
                }
            }

            public UIImage _Image{
                get{
                    return imageView.Image;
                }
                set{
                    imageView.Image = value;
                }
            }

            [Export("initWithFrame:")]
            ShapeCell(RectangleF frame) : base(frame)
            {
                label = new UILabel()
                {
                    Frame = new CoreGraphics.CGRect(10, 80, 60, 20),
                    TextColor = UIColor.Black,
                    AdjustsFontSizeToFitWidth = true,
                    Font = AasthaFontAndSize.R12(),
                    TextAlignment = UITextAlignment.Center
                 };

                imageView = new UIImageView()
                {
                    Frame = new CoreGraphics.CGRect(10, 10, 60, 60)
                };

                ContentView.AddSubviews(imageView,label);
            }
        }

UICollectionViewShapeSource.cs

class UICollectionViewShapeSource : UICollectionViewSource
        {
            public List<string> shapeList;
            public UICollectionViewShapeSource(List<string> shapeList)
            {
                this.shapeList = shapeList;
            }

            public override UICollectionViewCell GetCell(UICollectionView collectionView, Foundation.NSIndexPath indexPath)
            {
                var shapeCell = (ShapeCell)collectionView.DequeueReusableCell(ShapeCell.CellId, indexPath);
                shapeCell.NameText = shapeList[indexPath.Row];
                shapeCell._Image = UIImage.FromBundle("login_arrow");
                return shapeCell;
            }

            public override nint GetItemsCount(UICollectionView collectionView, nint section)
            {
                return shapeList.Count;
            }

            public override void ItemHighlighted(UICollectionView collectionView, NSIndexPath indexPath)
            {
                base.ItemHighlighted(collectionView, indexPath);
            }

            public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
            {
                base.ItemSelected(collectionView, indexPath);
            }
        }

1 Ответ

0 голосов
/ 22 ноября 2018

У меня такая же проблема, чтобы заставить его работать с UICollectionViewSource тоже. Законченное использование UICollectionViewDelegateFlowLayout

public class MyCustomViewDelegate : UICollectionViewDelegateFlowLayout
{    
    public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
    {
       // Do something here.
    }
}

Чтобы использовать это.

Delegate = new MyCustomViewDelegate();
...