我已经阅读了很多有关 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;
关于ios - UICollectionView Xamarin.iOS,带按钮的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34960285/