George
George

Reputation: 161

UICollectionview click event for UISwitch

In the image below the UISwitches below are housed in a collection view. However, I am currently facing a problem where. If I select a switch in the top for example in Roof Structure I select the NON-SABS APPROVED PRODUCT switch when I scroll down another switch for which was not in the view has been selected.

I have followed the following steps to ascertain how man click events are being fired. I used the console to see the output when a switch is selected. The results showed that in some instances the switch required switch is only fired as the third event and the other two events that fire are switches that are not in the users view.

To try and solve this issue I tried solve the issue by assigning a negative click event to the switch. This has so far not worked, see the code below

The code for the Switch event

public void btnQuestionAnswer_Click(object sender, EventArgs e)
            {
                UITableViewRowSwitch btnQuestionAnswer = (UITableViewRowSwitch)sender;


                    if ((btnQuestionAnswer.section.HasValue) && (btnQuestionAnswer.row.HasValue))
                    {

                        db_QuestionAnswer questionAnswer = questionDataModel[btnQuestionAnswer.section.Value].QuestionAnswers[btnQuestionAnswer.row.Value];

                        //Console.Write(questionAnswer.Answer);
                        Console.WriteLine(questionAnswer.Answer);

                        if ((btnQuestionAnswer.On))
                        {
                            if (questionDataModel[btnQuestionAnswer.section.Value].ComplianceIndicator)
                            {
                                foreach (db_QuestionAnswer QA in questionDataModel[btnQuestionAnswer.section.Value].QuestionAnswers)
                                {
                                    QA.isTicked = false;
                                }
                            }
                            questionAnswer.isTicked = true;
                            // ((UICollectionView)btnQuestionAnswer.relatedView).ReloadData ();
                        }
                        else
                        {
                            questionAnswer.isTicked = false;
                        }
                    }

                    else
                    {
                        btnQuestionAnswer.On = !btnQuestionAnswer.On;
                    }
                    var element = count.ToString();

            }

public override UICollectionViewCell GetCell (UICollectionView collectionView, NSIndexPath indexPath) { UIView cell;

                if (questionDataModel[indexPath.Section].ComplianceIndicator) 
                {
                    cell = collectionView.DequeueReusableCell (QuestionUICollectionViewDelegateDataSource.complianceQuestionCellId, indexPath);
                }
                else
                {
                    cell = collectionView.DequeueReusableCell (QuestionUICollectionViewDelegateDataSource.questionCellId, indexPath);
                }

                int row = indexPath.Row;

                UILabel lblQuestionAnswer = (UILabel)cell.ViewWithTag (1);
                UITableViewRowSwitch btnQuestionAnswer = (UITableViewRowSwitch)cell.ViewWithTag (2);



                btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click;

                btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click;

                btnQuestionAnswer.ValueChanged += btnQuestionAnswer_Click;
                if (row < questionDataModel [indexPath.Section].QuestionAnswers.Count) 
                {
                    lblQuestionAnswer.Text = questionDataModel[indexPath.Section].QuestionAnswers[indexPath.Row].Answer;

                    btnQuestionAnswer.section = indexPath.Section;
                    btnQuestionAnswer.row = indexPath.Row;


                    btnQuestionAnswer.On = questionDataModel [indexPath.Section].QuestionAnswers [indexPath.Row].isTicked;

                    //----------------TODO----------------//
                    // ---- 
                    btnQuestionAnswer.ValueChanged += btnQuestionAnswer_Click;

                    btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click;
                    //if (!btnQuestionAnswer.hasEvent)
                    {
                        btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click;
                        //btnQuestionAnswer.ValueChanged -= btnQuestionAnswer_Click;
                        btnQuestionAnswer.ValueChanged += btnQuestionAnswer_Click;

                        //btnQuestionAnswer.hasEvent = true;
                    }

                    btnQuestionAnswer.relatedView = collectionView;
                    if (questionDataModel [indexPath.Section].isLocked) 
                    {
                        btnQuestionAnswer.Enabled = false;
                    } 
                    else 
                    {
                        btnQuestionAnswer.Enabled = true;
                    }

                    lblQuestionAnswer.Hidden = false;
                    btnQuestionAnswer.Hidden = false;
                } 
                else 
                {
                    lblQuestionAnswer.Hidden = true;
                    btnQuestionAnswer.Hidden = true;
                }

                if (controller.loggedInUser.UserType != "Inspector")
                {
                    btnQuestionAnswer.Enabled = false;
                }

                return (UICollectionViewCell)cell;
            }

enter image description here

Upvotes: 0

Views: 628

Answers (1)

JoeTomks
JoeTomks

Reputation: 3276

You would most likely need to override the following methods of the UICollectionViewCell:

    public override void PrepareForReuse()
    {
        base.PrepareForReuse();
    }

    public override void AwakeFromNib()
    {
        base.AwakeFromNib();

    }

The problem you're seeing is that the collection view is correctly 'reusing' cell's that have already been loaded into memory, as such it keeps the current 'state' of the controls that are within the cells UIView. So in the prepare for reuse you need to essentially reset all of the UISwitches to their defaults. You could then use the 'AwakeFromNib' override to ensure that the cells that you want to keep the state of their switches are set accordingly. You would probably be wise to have a few bool values within the UIcollectionViewCell that keep hold of the current state of the switches and then apply them in 'AwakeFromNib'. Hope this helps.

Edit:

As it appears you are using static cells at the moment, here is a link to xamarins documentation on deriving your own custom cells from the base UICollectionViewCell. Link

This should give you the information you need to derive the class and give you access to the methods I mentioned above, allowing you a lot more control over what appears and in what state etc.

Upvotes: 1

Related Questions