[英]Get data from selected cell in collection vew cell
I have a functional collection view where I store the employee ID and I need to get that data from the cell when it's selected.我有一个功能集合视图,我在其中存储员工 ID,并且我需要在选择单元格时从单元格中获取该数据。 I created a UICollectionViewDelegate
where I get the index of the selected view in the view collection, but the cell
returned does not have the property where I store the employee id.我创建了一个UICollectionViewDelegate
在其中获取视图集合中所选视图的索引,但返回的cell
没有我存储员工 ID 的属性。 I've looked all over for a solution, but nothing addresses my specific issue and nothing I've tried to date works.我已经到处寻找解决方案,但没有解决我的具体问题,而且我尝试过的任何工作都没有。 My code:我的代码:
The collection view:集合视图:
public partial class Participants : UIViewController
{
public User MyUser;
public string company = "";
public string department = "";
public string section = "";
public Participants(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
DataInterfaceWeb.DataInterface myService = new DataInterfaceWeb.DataInterface();
DataSet dbParticipants = myService.GetParticipantsWithPhoto(MyUser.Username, MyUser.Password, company, department, section, "CURRENT");
View.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
var height = this.NavigationController.NavigationBar.Bounds.Height;
var width = this.NavigationController.NavigationBar.Bounds.Width;
viewParticipants.AddConstraints(
banner.AtTopOf(View, height),
banner.AtRightOf(View, 0),
banner.AtLeftOf(View, 0),
lblTitle.Below(banner, 0),
lblTitle.WithSameWidth(banner)
, cvParticipants.Below(lblTitle, 0),
cvParticipants.WithSameWidth(lblTitle),
cvParticipants.AtLeftOf(View, 10),
cvParticipants.AtRightOf(View, 10),
cvParticipants.AtBottomOf(View,10)
);
cvParticipants.CollectionViewLayout = new UICollectionViewFlowLayout()
{
ItemSize = new CGSize((float)UIScreen.MainScreen.Bounds.Size.Width / 2.15f, (float)UIScreen.MainScreen.Bounds.Size.Width / 2.15f),
HeaderReferenceSize = new CGSize(25, 25),
SectionInset = new UIEdgeInsets(0, 0, 0, 0),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
MinimumInteritemSpacing = 0, // minimum spacing between cells
MinimumLineSpacing = 0,
// minimum spacing between rows if ScrollDirection is Vertical or between columns if Horizontal
};
cvParticipants.RegisterClassForCell(typeof(ParticipantCell), ParticipantCell.participantCellId);
cvParticipants.Source = new CVSource(dbParticipants);
cvParticipants.Delegate = new ParticipantDelegate();
cvParticipants.BackgroundColor = UIKit.UIColor.FromRGB(255, 215, 0);
cvParticipants.ReloadData();
lblTitle.Text = team + " Participants";
}
}
The cell class:单元格 class:
class ParticipantCell : UICollectionViewCell
{
UIImageView imageView { get; set; }
UILabel lblName { get; set; }
string EmployeeID { get; set; }
public static NSString participantCellId = new NSString("ParticipantCell");
[Export("initWithFrame:")]
public ParticipantCell(CGRect frame) : base(frame)
{
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.ContentMode = UIViewContentMode.ScaleAspectFit;
lblName = new UILabel();
ContentView.AddSubview(new UIView() { imageView, lblName });
}
public void SetImage (UIImage image, string name, string playerID)
{
EmployeeID = employeeID;
imageView.Image = image;
imageView.Frame = new CGRect(0, 0, ContentView.Bounds.Width, ContentView.Bounds.Height);
lblName.Text = name;
lblName.Frame = new CGRect(0, ContentView.Bounds.Height, ContentView.Bounds.Width, 26);
}
}
The view controller and source classes:视图 controller 和源类:
class ParticipantViewController : UIViewController
{
static NSString participantCellId = new NSString("ParticipantCell");
public DataSet Participants;
UICollectionView collectionView;
CVSource source;
public ParticipantViewController(UICollectionViewLayout layout, DataSet pParticipants) //: base(layout)
{
Participants = pParticipants;
collectionView = new UICollectionView(UIScreen.MainScreen.Bounds, layout);
collectionView.ContentSize = View.Frame.Size;
source = new CVSource(pParticipants);
collectionView.RegisterClassForCell(typeof(ParticipantCell), participantCellId);
collectionView.Source = source;
}
public override void ViewDidLoad()
{
View = collectionView;
}
}
class CVSource : UICollectionViewSource
{
DataSet Participants = new DataSet();
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return Participants.Tables[0].Rows.Count;
}
public CVSource(DataSet pParticipants)
{
Participants = pParticipants;
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var participantCell = (ParticipantCell)collectionView.DequeueReusableCell(ParticipantCell.participantCellId, indexPath);
string name = Participants.Tables[0].Rows[indexPath.Row]["Name"].ToString();
byte[] imageBytes = (byte[])Participants.Tables[0].Rows[indexPath.Row]["Photo"];
string employeeid= Participants.Tables[0].Rows[indexPath.Row]["employeeid"].ToString();
UIImage Mybitmap = GetImagefromByteArray(imageBytes);
participantCell.SetImage(Mybitmap, name, playerid); // MaxResizeImage(Mybitmap, 200f, 200f);
return participantCell;
}
public static UIImage GetImagefromByteArray(byte[] imageBuffer)
{
NSData imageData = NSData.FromArray(imageBuffer);
return UIImage.LoadFromData(imageData);
}
}
Finally the delegate where I capture the selected cell:最后是我捕获所选单元格的代表:
class ParticipantDelegate : UICollectionViewDelegate
{
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = collectionView.CellForItem(indexPath);
}
}
The cell
does not contain any of the properties defined in participantCell and I need the information.该cell
不包含在participantCell 中定义的任何属性,我需要这些信息。 Everything else works as expected.其他一切都按预期工作。
The right way should be passing the Participants
to your delegate
class as what you did in the CVSource
:正确的方法应该是将Participants
传递给您的delegate
class ,就像您在CVSource
中所做的那样:
class ParticipantDelegate : UICollectionViewDelegate
{
DataSet Participants = new DataSet();
public ParticipantDelegate(DataSet pParticipants)
{
Participants = pParticipants;
}
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = collectionView.CellForItem(indexPath);
//get the data
var cellData = Participants.Tables[0].Rows[indexPath.Row];
//For example
string name = Participants.Tables[0].Rows[indexPath.Row]["Name"].ToString();
}
}
And set delegate:并设置委托:
cvParticipants.Delegate = new ParticipantDelegate(dbParticipants);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.