Error in UICollectionView button handler

EstherMabongaEstherMabonga Esther MabongaUSUniversity ✭✭

I have some forward and back arrow buttons in my header in the UICollectionView as shown below:

When the user clicks on the back arrow, I display the dates of the previous month, and the forward arrow, shows the next month, just like you would see on a normal calendar.

I have implemented very simple logic to show previous months, but only three months history. So they should only be able to view 3 months back from the current month.

This should be pretty straight forward but I can't seem to get it working correctly. I have a counter which I check to make sure we're not past the 3 month-back check once the button was clicked, then I increase it if we're not there yet, and reload the UICollecitonView to display the previous month accordingly.

Below is my code:

`public override UICollectionReusableView GetViewForSupplementaryElement(UICollectionView collectionView, Foundation.NSString elementKind, Foundation.NSIndexPath indexPath)
    {

        UICollectionReusableView view = null;

        try
        {

            switch (elementKind)
            {

                case "UICollectionElementKindSectionHeader":

                    var headerView = (Header)collectionView.DequeueReusableSupplementaryView(elementKind, "header", indexPath);

                    view = headerView;

                    headerView.labelDate.Text = date.ToString("Y");

                    headerView.btnPrevious.TouchUpInside += HandlePreviousButtonEventHandler;

                    headerView.btnNext.TouchUpInside += HandleNextButtonEventHandler;

                    break;

                case "UICollectionElementKindSectionFooter":

                    var footerView = (Footer)collectionView.DequeueReusableSupplementaryView(elementKind, "footer", indexPath);
                    //var footerView = collectionView.DequeueReusableSupplementaryView(elementKind, "footer", indexPath);

                    view = footerView;


                    break;

            }


        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message + ex.StackTrace);

        }
        return view;
    }


  void HandlePreviousButtonEventHandler(object sender, EventArgs e)
    {
        try
        {

        if (dataManager.backCounter >= 4)
                {

                    calendarController.DisplayAlert("", "Only three months history is available!", "");

                }
                else {

                    calendarController.mainCollectionView.Source = new CalendarViewDataSource(calendarController, new DateTime(date.AddMonths(-1).Year, date.AddMonths(-1).Month, 1));

                    calendarController.mainCollectionView.ReloadData();

                    previousButtonWasClicked = true;
                    dataManager.backCounter++;

                }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + ex.StackTrace);
        }

    }

`
My first challenge:

  1. After stepping through the code, I noticed that it checks the if statement twice in each button click. So after checking the condition and incrementing the counter, it should stop there until the user clicks the previous button again.

In this case, it checks the condition one more time and increments the counter again, always showing me only 2 months back, instead of 3.

  1. The next button doesn't have any limits. If I scroll all the way to June this year, and would like to scroll back to the current Month - January, it will implement the 3 month check and stop at March this year.

How can I correctly implement this logic?

Sign In or Register to comment.