2016-01-23 92 views
0

我已閱讀許多教程那裏爲UICollectionView爲iOS和Xamarin.iOS 大家已經顯示出相同的樣品教程與一個圖像內UICollectionViewCellUICollectionView Xamarin.iOS,與按鈕個人細胞

希望的輸出

UICollectionViewCell與定製設計 GridView

我做了什麼

這是我所取得的成就迄今..請無視糟糕的設計:P UICollectionView in Xamarin.iOS

問題

當我點擊簿按鈕,它觸發事件多次。奇怪的是,它並不一致,有時它會觸發一次一段時間的兩倍或一段時間的三倍或任何...

代碼段

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提示,那將會很好..

+1

嘗試先取消訂閱該活動,然後訂閱以防止多個觸發器。告訴我它是否適用於發佈爲答案,我現在沒有Mac現在來測試它。 –

+0

謝謝,這似乎工作.. :) –

回答

1

解決這個問題的一種可能的方法是在訂閱它之前取消訂閱事件,這樣它將防止多次呼叫,特別是如果這個項目在列表中被回收。

cell.BookButton.TouchUpInside -= Cell_BookButton_TouchUpInside; 
cell.BookButton.TouchUpInside += Cell_BookButton_TouchUpInside;