UICollectionView Xamarin.iOS,带按钮的自定义单元格
我已经阅读了很多iOS和Xamarin.iOS的UICollectionView教程 每个人都在UICollectionViewCell中展示了相同的示例教程和一幅图像 所需输出UICollectionView Xamarin.iOS,带按钮的自定义单元格,ios,xamarin,xamarin.ios,uicollectionview,Ios,Xamarin,Xamarin.ios,Uicollectionview,我已经阅读了很多iOS和Xamarin.iOS的UICollectionView教程 每个人都在UICollectionViewCell中展示了相同的示例教程和一幅图像 所需输出 List<MeetingRoom> data = new List<MeetingRoom>(); async public override void ViewDidLoad() { base.ViewDidLoad(); data = await DashboardSer
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);
}
具有自定义设计的UICollectionViewCell
我所做的
这就是我到目前为止所取得的成就。。请忽略糟糕的设计:P
问题
当我点击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);
}
列表数据=新列表();
异步公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
data=wait DashboardServices.getMeetingRooms(DateTime.Now);
MeetingRoomCollection.WeakDataSource=此;
}
公共nint GetItemsCount(UICollectionView collectionView,nint部分)
{
var count=data.count;
返回计数;
}
公共UICollectionViewCell GetCell(UICollectionView collectionView,NSIndexPath indexPath)
{
var cell=(MeetingRoomCell)collectionView.DequeueReusableCell(“cell”,indexath);
cell.RoomName.Text=数据[indexPath.Row].Room\u名称;
cell.Status.Text=数据[indexPath.Row]。可用性;
cell.BookButton.Tag=indexPath.Row;
cell.BookButton.TouchUpInside+=cell\u BookButton\u TouchUpInside;
返回单元;
}
无效单元格\u BookButton\u TouchUpInside(对象发送器,事件参数e)
{
var x=发送方作为UIButton;
WriteLine(“索引:{0},状态:{1}”,(int)x.Tag,数据[(int)x.Tag].Availability);
这个。表演格(“segue_bookRoom”,这个);
}
请建议需要做什么来解决这个问题,任何参考可用的教程将不胜感激。如果有Xamarin.iOS提示就好了。解决此问题的一个可能方法是在订阅事件之前取消订阅,这样可以防止多次调用,特别是当此项目在列表中循环使用时
cell.BookButton.TouchUpInside -= Cell_BookButton_TouchUpInside;
cell.BookButton.TouchUpInside += Cell_BookButton_TouchUpInside;
解决此问题的一种可能方法是在订阅事件之前取消订阅,这样可以防止多次调用,特别是当此项目在列表中循环使用时
cell.BookButton.TouchUpInside -= Cell_BookButton_TouchUpInside;
cell.BookButton.TouchUpInside += Cell_BookButton_TouchUpInside;
尝试先取消订阅活动,然后再订阅,以防止多个触发器<代码>cell.BookButton.TouchUpInside-=cell\u BookButton\u TouchUpInside;cell.BookButton.TouchUpInside+=cell\u BookButton\u TouchUpInside代码>告诉我作为答案发布是否对您有效,我现在没有Mac来测试它。谢谢,这似乎有效:)尝试先取消订阅活动,然后再订阅,以防止多个触发器<代码>cell.BookButton.TouchUpInside-=cell\u BookButton\u TouchUpInside;cell.BookButton.TouchUpInside+=cell\u BookButton\u TouchUpInside代码>告诉我作为答案发布是否对您有效,我现在没有Mac来测试它。谢谢,这似乎有效:)