【问题标题】:UICollectionview click event for UISwitchUISwitch 的 UICollectionview 点击事件
【发布时间】:2017-03-09 15:28:28
【问题描述】:

在下图中,下面的 UISwitches 位于集合视图中。但是,我目前面临的问题在哪里。如果我在顶部选择一个开关,例如在屋顶结构中,当我向下滚动另一个不在视图中的开关时,我会选择 NON-SABS APPROVED PRODUCT 开关。

我已按照以下步骤确定如何触发手动点击事件。选择开关时,我使用控制台查看输出。 结果表明,在某些情况下,switch required switch 仅作为第三个事件触发,而触发的另外两个事件是不在用户视图中的开关。

为了尝试解决这个问题,我尝试通过为开关分配一个否定点击事件来解决这个问题。 到目前为止,这还没有奏效,请参见下面的代码

Switch事件的代码

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 单元格;

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

【问题讨论】:

    标签: ios xamarin.ios uicollectionview uicollectionviewcell


    【解决方案1】:

    您很可能需要覆盖 UICollectionViewCell 的以下方法:

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

    您看到的问题是集合视图正确地“重用”了已加载到内存中的单元格,因此它保留了单元格 UIView 中控件的当前“状态”。因此,在准备重用时,您需要将所有 UISwitches 重置为默认值。然后,您可以使用“AwakeFromNib”覆盖来确保相应地设置要保持其开关状态的单元格。明智的做法是在 UIcollectionViewCell 中保留一些布尔值来保持开关的当前状态,然后将它们应用到“AwakeFromNib”中。希望这会有所帮助。

    编辑:

    目前看来您正在使用静态单元格,这里有一个指向 xamarins 文档的链接,该文档介绍了从基本 UICollectionViewCell 派生您自己的自定义单元格。 Link

    这应该为您提供派生类所需的信息,并让您可以访问我上面提到的方法,让您可以更好地控制出现的内容和状态等。

    【讨论】:

    • 如何添加 PrepareForReuse 我得到以下错误没有找到合适的错误来修改
    • 您是否在为您的单元格使用自定义类?还是使用静态单元格?例如。公共部分类 MyDerivedCell : UICollectionViewCell
    • 类里程碑视图DelegateDataSource : UITableViewSource{} 和类 QuestionUICollectionViewDelegateDataSource : UICollectionViewSource {}
    • 我明白了,基本上要做你想做的事,你需要从它的基础派生你自己的单元类,这样你就可以访问上述方法。但这是我知道的唯一方法,当集合视图将可重复使用的单元格出队时,您可以保持正确的显示。
    • 我有点迷茫,在使用 Xamarin 和 iOS 时我仍在寻找立足点。你能指出我如何解决这个问题的正确方向吗?
    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多