[英]Xamarin.iOS UICollectionView with custom UICollectionViewSource crashes
[英]UICollectionView Xamarin.iOS, Custom cell with button
我已经阅读了很多关于 iOS 和 Xamarin.iOS 的 UICollectionView 的教程 每个人都在 UICollectionViewCell 中展示了一张图片的相同示例教程
期望的输出
我做了什么
问题
当我点击 Book 按钮时,它会多次触发该事件。 奇怪的是它不一致,有时它只会触发一次有时两次或有时三次或其他什么......
代码片段
List<MeetingRoom> data = new List<MeetingRoom>();
async public override void ViewDidLoad()
{
base.ViewDidLoad();
data = await DashboardServices.getMeetingRooms(DateTime.Now);
MeetingRoomCollection.WeakDataSource = this;
}
public nint GetItemsCount(UICollectionView collectionView, nint section)
{
var count = data.Count;
return count;
}
public UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = (MeetingRoomCell)collectionView.DequeueReusableCell("cell", indexPath);
cell.RoomName.Text = data[indexPath.Row].Room_Name;
cell.Status.Text = data[indexPath.Row].Availability;
cell.BookButton.Tag = indexPath.Row;
cell.BookButton.TouchUpInside += Cell_BookButton_TouchUpInside;
return cell;
}
void Cell_BookButton_TouchUpInside(object sender, EventArgs e)
{
var x = sender as UIButton;
Debug.WriteLine("index : {0}, status : {1}", (int)x.Tag, data[(int)x.Tag].Availability);
this.PerformSegue("segue_bookRoom", this);
}
请建议需要做什么来解决这个问题,任何对可用教程的参考都将不胜感激。 如果有 Xamarin.iOS 提示,那就太好了。
解决此问题的一种可能方法是在订阅之前取消订阅事件,这样可以防止多次调用,特别是如果该项目在列表中被回收。
cell.BookButton.TouchUpInside -= Cell_BookButton_TouchUpInside;
cell.BookButton.TouchUpInside += Cell_BookButton_TouchUpInside;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.