简体   繁体   中英

UICollectionView Xamarin.iOS, Custom cell with button

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 网格视图

What I have done

This is what I have achieved so far.. please ignore the bad design:P Xamarin.iOS 中的 UICollectionView

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..

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM