gpt4 book ai didi

ios - UISwitch 的 UICollectionview 点击事件

转载 作者:行者123 更新时间:2023-11-29 00:27:07 25 4
gpt4 key购买 nike

在下图中,下方的 UISwitch 位于 Collection View 中。然而,我目前面临一个问题。如果我在顶部选择一个开关,例如在“屋顶结构”中,当我向下滚动另一个不在 View 中的开关时,我会选择“非 SABS 批准的产品”开关。

我已按照以下步骤确定如何触发 man click 事件。选择开关后,我使用控制台查看输出。结果表明,在某些情况下,需要切换的开关仅作为第三个事件触发,而触发的其他两个事件是不在用户 View 中的开关。

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

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

enter image description here

最佳答案

您很可能需要重写 UICollectionViewCell 的以下方法:

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

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

}

您看到的问题是 Collection View 正确地“重用”了已经加载到内存中的单元格,因此它保持了单元格 UIView 中控件的当前“状态”。因此,在准备重用时,您基本上需要将所有 UISwitch 重置为其默认值。然后,您可以使用“AwakeFromNib”覆盖来确保相应地设置您想要保持其开关状态的单元格。在 UIcollectionViewCell 中设置一些 bool 值可能是明智的,这些值保持开关的当前状态,然后将它们应用到“AwakeFromNib”中。希望这会有所帮助。

编辑:

您目前似乎正在使用静态单元格,这里是 xamarins 文档的链接,介绍了如何从基础 UICollectionViewCell 派生您自己的自定义单元格。 Link

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

关于ios - UISwitch 的 UICollectionview 点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42699240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com