簡體   English   中英

如何以編程方式在collectionview中選擇項目?

[英]How to select item in collectionview programmatically?

我有一個UICollectionView UICollectionViewdatasource是一個NSArray的學生(來自CoreData )。 我希望當前學生項目被selected / highlighted

我怎樣才能做到這一點? 我知道有一種方法:

- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath 
                     animated:(BOOL)animated 
               scrollPosition:(UICollectionViewScrollPosition)scrollPosition;

它將NSIndexPathUICollectionViewScrollPosition作為參數。

我有數據源(從CoreData填充的學生NSArray )和要selected的學生對象。

那么,我如何獲得NSIndexPathUICollectionViewScrollPosition 或者有沒有其他方法可以突出顯示項目?

您可以在[self.collectionView reloadData]之后簡單地使用它

[self.collectionView 
   selectItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] 
                animated:YES
          scrollPosition:UICollectionViewScrollPositionCenteredVertically];

其中index是所選學生的索引號。

迅速

let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionView.ScrollPosition.centeredHorizontally)

根據您的問題,我假設您只有一個選定的學生,我對用戶可以選擇的一組圖標做了類似的事情。 首先在視圖中確實加載了我所做的:

override func viewDidLoad() {
    super.viewDidLoad()

    iconCollectionView.delegate = self
    iconCollectionView.dataSource = self
    iconCollectionView.allowsMultipleSelection = false
    iconCollectionView.selectItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), animated: false, scrollPosition: .None)
}

這里我默認選擇第一個單元格,您將使用StudentArray.indexOf來獲取您選擇的學生索引。 然后顯示我選擇了哪個項目:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = iconCollectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! IconCollectionViewCell
    cell.imageView.image = UIImage(named: imageResourceNames.pngImageNames[indexPath.row])
    if cell.selected {
        cell.backgroundColor = UIColor.grayColor()
    }
    return cell
}

這在第一次顯示集合時調用,然后對選擇的更改做出反應:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.grayColor()
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.cellForItemAtIndexPath(indexPath)?.backgroundColor = UIColor.clearColor()
}

顯然有很多方法可以顯示單元格選擇,我的方法很簡單,但這就是我需要做的。

編輯:自從發布上述內容后,我意識到在單元格類中添加一個觀察者來selected更簡單:

class IconCollectionViewCell: UICollectionViewCell {

...

    override var selected: Bool {
        didSet {
            backgroundColor = selected ? UIColor.grayColor() : UIColor.clearColor()
        }
    }
}

有了這個,就不需要處理didSelectdidDeselect或檢查didDeselect中的cellForItemAtIndexPath ,單元格會自動執行。

我知道太晚了,但只是為了記錄,對於第 y 部分中的項目 x:

 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:x inSection:y];

如果您只想以編程方式突出顯示一個項目,則需要在集合視圖本身上調用 selectItemAtIndexPath 方法,例如:

[theCollectionView selectItemAtIndexPath:indexPath
    animated:YES scrollPosition:UICollectionViewScrollPositionCenteredVertically];

如果您想以編程方式模擬選擇一個項目,並且您在 do select 中調用了代碼,您需要在視圖控制器本身上調用 selectItemAtIndexPath 方法並將您的集合視圖作為參數傳遞給它,例如:

[theViewController collectionView:theCollectionView didSelectItemAtIndexPath:indexPath];

現在可以肯定的是,為了選擇一個項目並調用 did select 代碼,您需要同時調用兩者:D

[theCollectionView selectItemAtIndexPath:indexPath
    animated:YES scrollPosition:UICollectionViewScrollPositionCenteredVertically];

[theViewController collectionView:theCollectionView didSelectItemAtIndexPath:indexPath];

SWIFT 版本:

let indexPath = IndexPath(item: x, section: y)
theCollectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredVertically)
theViewController.collectionView(theCollectionView, didSelectItemAt: indexPath)

請享用!

簽名有變化。

didSelectItemAtIndexPath  to didSelectItemAt 

你檢查一下:

_ collectionView

請試試這個:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){

        print(" selected",)
    }

快速 5

collView.selectItem(at: NSIndexPath(item: 0, section: 0) as IndexPath, animated: false, scrollPosition: .centeredHorizontally)
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
    paymentHeaderCollectionView.selectItem(at: indexPathForFirstRow, animated: false, scrollPosition: UICollectionViewScrollPosition.left)
    self.collectionView(paymentHeaderCollectionView, didSelectItemAt: indexPathForFirstRow)

使用斯威夫特:

您可以簡單地執行此操作來獲取collectiveView 的索引並使用該鍵從您的數組中獲取數據

collectionView.indexPathsForSelectedItems?.last?.row

這為您提供所選項目的索引作為整數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM