Arrays 正在更新整数数组
我有一组模拟数据,如下所示:Arrays 正在更新整数数组,arrays,swift,uicollectionview,Arrays,Swift,Uicollectionview,我有一组模拟数据,如下所示: var demoInit=[(“姓名”、“金额”、“地点”),(“姓名”、“金额”、“地点”),(“姓名”、“金额”、“地点”)] 我在集合视图中使用它,我希望我的用户不要选择三个以上,我做到了: func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let selectedCell = collecti
var demoInit=[(“姓名”、“金额”、“地点”),(“姓名”、“金额”、“地点”),(“姓名”、“金额”、“地点”)]
我在集合视图中使用它,我希望我的用户不要选择三个以上,我做到了:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("interestAsset", forIndexPath: indexPath) as! InterestCell
if selectedItems.count < 3 {
selectedItems += [indexPath.row]
selectedCell.holderView.alpha = 0.5
selectedCell.holderView.backgroundColor = UIColor.greenColor()
selectedCell.checkMark.hidden = false
}
}
func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indepath:nsindepath){
让selectedCell=collectionView.dequeueReusableCellWithReuseIdentifier(“InterestaSet”,forIndexPath:indexPath)作为!InterestCell
如果选择editems.count<3{
selectedItems+=[indexPath.row]
selectedCell.holderView.alpha=0.5
selectedCell.holderView.backgroundColor=UIColor.greenColor()
selectedCell.checkMark.hidden=false
}
}
如果您注意到这一行,
selectedItems+=[indexPath.row]
是我存储UICollectionView
的所选indexPath
的地方。但是我遇到的问题是,我想实现取消选择,但要实现它,我需要从selectedItems
数组中删除特定存储的indepath
。我该怎么做?谢谢如果这是你想要实现的,就用这个:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("interestAsset", forIndexPath: indexPath) as! InterestCell
if selectedItems.count < 3 {
selectedItems += [indexPath.row]
selectedCell.holderView.alpha = 0.5
selectedCell.holderView.backgroundColor = UIColor.greenColor()
selectedCell.checkMark.hidden = false
}
for (var index = 0; index < 3; ++index) {
if(selectedItems[index] == indexPath.row)
{
selectedItems.removeAtIndex(index)
}
}
}
func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indepath:nsindepath){
让selectedCell=collectionView.dequeueReusableCellWithReuseIdentifier(“InterestaSet”,forIndexPath:indexPath)作为!InterestCell
如果选择editems.count<3{
selectedItems+=[indexPath.row]
selectedCell.holderView.alpha=0.5
selectedCell.holderView.backgroundColor=UIColor.greenColor()
selectedCell.checkMark.hidden=false
}
对于(var指数=0;指数<3;++指数){
if(selectedItems[index]==indexPath.row)
{
selectedItems.removeAtIndex(索引)
}
}
}
说明:
您正在使用for循环来循环3个选定项目。如果其中一个与您的indexPath相等,那么它将跳转到If函数内部。
在if函数内部,它只是删除selectedItem表单selectedItems
如果有效,请给我写信…您可以使用
array.removateIndex
方法从数组中删除
一个例子:
var intArray = [1,5,6,8,10]
//Remove 10
intArray.removeAtIndex(intArray.indexOf(10))
print("\(intArray)") // [1,5,6,8]
无需在
UICollectionView
中手动跟踪所选单元格
只需调用collectionView.indexPathsForSelectedItems()
即可检索与所选单元格相关的索引列表
你的代码变成这样了
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let selectedCell = collectionView.dequeueReusableCellWithReuseIdentifier("interestAsset", forIndexPath: indexPath) as! InterestCell
let selectedItems = collectionView.indexPathsForSelectedItems()?.count ?? 0
if selectedItems < 3 {
selectedCell.holderView.alpha = 0.5
selectedCell.holderView.backgroundColor = UIColor.greenColor()
selectedCell.checkMark.hidden = false
}
}
func collectionView(collectionView:UICollectionView,didSelectItemAtIndexPath indepath:nsindepath){
让selectedCell=collectionView.dequeueReusableCellWithReuseIdentifier(“InterestaSet”,forIndexPath:indexPath)作为!InterestCell
让selectedItems=collectionView.indexPathsForSelectedItems()?.count±0
如果选择EdItems<3{
selectedCell.holderView.alpha=0.5
selectedCell.holderView.backgroundColor=UIColor.greenColor()
selectedCell.checkMark.hidden=false
}
}
看看我的答案,这应该行得通。。。