基于UICollectionView Xamarin.iOS中选定的单元格更新UIViewController中标签的文本
这个问题与Xamarin.iOS有关。我在UIViewController中有一个UICollectionView和一个标签。我已设置UICollectionView的源代码。在其中,我覆盖了ItemSelected和ItemSelected方法,以突出显示所选项目。我还可以在UICollectionView中获取所选单元格的索引。但我不知道如何将此索引传递给父UIViewController,以便根据UICollectionView中选定的单元格更新标签基于UICollectionView Xamarin.iOS中选定的单元格更新UIViewController中标签的文本,xamarin.ios,uicollectionview,Xamarin.ios,Uicollectionview,这个问题与Xamarin.iOS有关。我在UIViewController中有一个UICollectionView和一个标签。我已设置UICollectionView的源代码。在其中,我覆盖了ItemSelected和ItemSelected方法,以突出显示所选项目。我还可以在UICollectionView中获取所选单元格的索引。但我不知道如何将此索引传递给父UIViewController,以便根据UICollectionView中选定的单元格更新标签 public override voi
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);
}
您可以首先在
ViewController
public NSIndexPath selectedIndex ;
然后,有两种方法可以将此索引传递给父UIViewController
首先,您可以在ViewController中实现UICollectionViewDataSource
和UICollectionViewDelegate
的方法。并在选择项目时设置成员的值。请参阅以下代码:
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);
}
如果确实希望在单个类中实现UICollectionViewDataSource
和UICollectionViewDelegate
的方法。初始化源时,将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中:
CustomCollectionView.Source=新建
newCustomCollectionSource(这是imageLocations)
您可以像
myindexath.Row
,myindexath一样获取所选单元格的索引。在ViewController中的
部分。谢谢@LucasZ。我会尝试一下,然后按照你的计划回来。谢谢你救了我。