UICollectionView Xamarin.iOS, Custom cell with button

Published

I have read many tutorial out there for UICollectionView for both iOS and Xamarin.iOS
Everybody has shown same sample tutorial with one image inside UICollectionViewCell

Desired output

UICollectionViewCell with custom design
GridView

What I have done

This is what I have achieved so far.. please ignore the bad design 😛
UICollectionView in Xamarin.iOS

Problem

When I Click on Book button, it triggers the event multiple times. The weird thing is that its not consistent, sometimes it will trigger only once some time twice or some time three times or whatever…

Code Snippet

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);
}

Please suggest what needs to be done to fix this issue, any reference to available tutorial would be appreciated. It will be great if Xamarin.iOS hint is there..

Source: Xamarin.ios Questions

Published
Categorised as Uncategorised Tagged , , ,

Answers

One possible way to solve this issue is to unsubscribe from event before subscribing it, this way it will prevent multiple calls especially if this item is recycled in list.

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

Jean Lehner

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq