2017-06-16 28 views
0

我正在開發播放器應用程序。我有播放列表,並在collectionview上有一個邊框,顯示當前正在播放的歌曲。歌曲結束後會自動播放下一首歌曲。如何在collectionView中查找indexpath

我想自動更新該單元作爲currentPlaying單元並將之前單元重置爲正常狀態。

因此,我想爲當前和上一首播放的歌曲找到indexPath。

func playingCompositionForReload(composition: Composition) { 
     composition.isPlaying = true   
     // find index path and reload one cell only 
} 

我怎麼能找到它?

+0

在我看來你的問題太普遍 – Robert

+1

創建IndexPath類型的三個變量:currentlyPlaying,previouslyPlayed和nextPlayable。每次你更新當前正在播放的歌曲,更新VC中的所有三個索引路徑。 –

+0

如果你問一個問題,你應該保持它的更新,關於你是否找到了答案,或者實際上使用了某人爲你發佈的答案。否則,人們會在你的歷史中看到你不接受答案,他們會停止回答。 – SomaMan

回答

0

你需要保持當前單元格的indexPath在VAR:

var currentlyActiveCellIndexPath: IndexPath 

當用戶選擇一個單元格,設置currentlyActiveCellIndexPath給該小區的indexPath(雖然這取決於你如何開始一切都關掉 - 你可能不需要這個,如果你與第一小區,例如自動啓動,那麼你只設置currentlyActiveCellIndexPath到IndexPath(項目:0,部分:0))

func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) { 
    self.currentlyActiveCellIndexPath = indexPath 
    // etc, etc 
} 

您需要實現某種委託模式(單元呼叫的地方)一個方法在它的委託上)讓你的控制代碼知道這首歌已經完成,&當它接到電話時,只需調用你在下一個單元格上創建的函數,你可以通過增加indexPath的項目來找到它。例如 -

// delegate method called by cell 

func songDidFinish() { 

    self.currentlyActiveCellIndexPath.item += 1 

    // Obviously you'll have to use whatever class you've created your cells as here, with the relevant methods, etc. 

    if let nextCell = self.collectionView.cellForItem(at: self.currentlyActiveCellIndexPath) as? SongPlayingCell { 
     nextCell.startPlaying() 
    } 
} 

推測,當一個單元格停止播放時,它可以設置自己的狀態。

如果你不確定如何使用代表,那是另一個問題,&有很多關於這方面的信息。

相關問題