gpt4 book ai didi

ios - 阵列达到限制后禁用选择

转载 作者:行者123 更新时间:2023-11-28 12:14:01 25 4
gpt4 key购买 nike

我正在使用 HTagView库来显示标签列表。我已经填充了标签,现在我想将选择限制为最多 3 个。这是我正在尝试的地方:

var selectedInterests = [Int]()

func tagView(_ tagView: HTagView, tagSelectionDidChange selectedIndices: [Int]) {

selectedInterests.removeAll()

for i in selectedIndices {
selectedInterests.append(i)

if selectedInterests.count > 3 {
print("limit reached")
selectedInterests.removeLast()
tagView.reloadData()
}
}
}

我试图删除数组的最后一项,但这也不起作用。大多数示例都基于表或 collectionView 的 indexPath 显示此示例。我如何使用两者中的任何一个来实现它?

最佳答案

如果已选择的标签数量大于 3,您需要做的就是立即取消选择标签。我还将数量作为一个变量,您可以轻松更改其值:

var maxTagsSelected = 3

func tagView(_ tagView: HTagView, tagSelectionDidChange selectedIndices: [Int]) {
if selectedIndices.count > maxTagsSelected {
tagView.deselectTagAtIndex(selectedIndices[maxTagsSelected])
}
}

变量 maxTagsSelected 将始终是 selectedIndices 中最后一个元素的索引。

关于ios - 阵列达到限制后禁用选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47472760/

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