根据UICollectionView Xamarin.iOS中选定的单元格更新UIViewController中的标签文本

时间:2018-09-13 13:17:40

标签: xamarin.ios uicollectionview

此问题与Xamarin.iOS有关。我在UIViewController中有一个UICollectionView和一个Label。我已经为UICollectionView设置了源代码。在其中,我重写了ItemSelected和ItemDeselected方法以突出显示所选项目。我还可以在UICollectionView中获取所选单元格的索引。但是我不知道如何将此索引传递给父UIViewController,因此我可以根据UICollectionView中选定的单元格更新标签。

public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = (newCustomCollectionViewCell)collectionView.CellForItem(indexPath);
        //cell.mainLabel.Alpha = 0.5f;
        cell.ImageView.Alpha = 0.9f;
        cell.ImageView.Layer.BorderWidth = 3.0f;
        cell.ImageView.Layer.BorderColor = UIColor.Red.CGColor;
        //base.ItemSelected(collectionView, indexPath);
        _selectedItems.Add(indexPath);
        ViewController.selectedIndex = indexPath.ToString();
        ViewController.cusLabel.Text = indexPath.ToString();

        //Console.WriteLine("Index of the highlighted cell is {0}", indexPath);
    }
    public override void ItemDeselected(UICollectionView collectionView, NSIndexPath indexPath)
    {

        var cell = (newCustomCollectionViewCell)collectionView.CellForItem(indexPath);
        //cell.mainLabel.Alpha = 0.5f;
        cell.ImageView.Alpha = 1f;
        cell.ImageView.Layer.BorderWidth = 3.0f;
        cell.ImageView.Layer.BorderColor = UIColor.White.CGColor;
        //base.ItemDeselected(collectionView, indexPath);
        _selectedItems.Remove(indexPath);
    }

上面的代码在newCustomCollectionSource.cs中

我想访问父视图控制器UIViewController中单元格的索引

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

        cusLabel.Frame = new CGRect(50, 50, 100, 30);
        cusLabel.BackgroundColor = UIColor.Gray;
        CustomColllectionView.RegisterClassForCell(typeof(newCustomCollectionViewCell), newCustomCollectionViewCell.CellID);
        CustomColllectionView.Source = new newCustomCollectionSource(imageLocations);
       }

1 个答案:

答案 0 :(得分:1)

您可以首先在ViewController

中声明一个成员变量
public NSIndexPath selectedIndex ;

然后,您可以通过两种方式将此索引传递给父UIViewController。

首先,您可以在ViewController中实现UICollectionViewDataSourceUICollectionViewDelegate的方法。并在选择项目时设置成员的值。请参考以下代码:

public partial class ViewController : UIViewController,IUICollectionViewDataSource,IUICollectionViewDelegate

。 。 。

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

      cusLabel.Frame = new CGRect(50, 50, 100, 30);
      cusLabel.BackgroundColor = UIColor.Gray;
      CustomColllectionView.RegisterClassForCell(typeof(newCustomCollectionViewCell), newCustomCollectionViewCell.CellID);
      CustomColllectionView.WeakDataSource= this;
 }

public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
 {
       …
       this. myIndexPath= NSIndexPath.FromRowSection(indexPath.Row, indexPath. Section);  
 }

如果您确实想在单个类中实现UICollectionViewDataSourceUICollectionViewDelegate的方法。初始化源代码时,将ViewController作为参数传递。

在您的newCustomCollectionSource.cs

private xxxViewController(your ViewController’s name) viewController;

public newCustomCollectionSource( xxxViewController viewController,…(other parameters)  )
 {
  . . . 
  this. viewController= viewController;
  . . .
 }

选择与上述相同的项目时,设置成员的值:

public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
 {
       …
       this.viewController.myIndexPath= NSIndexPath.FromRowSection(indexPath.Row, indexPath. Section);

 }

在您的ViewController中:

  

CustomColllectionView.Source =新   newCustomCollectionSource(this,imageLocations);

您可以像ViewController中的myIndexPath.RowmyIndexPath. Section一样获取所选单元格的索引。