Browse Source

搜索出来的音乐自动插入到第一位

jsonwang 3 years ago
parent
commit
ccf40a6a85
1 changed files with 8 additions and 6 deletions
  1. 8 6
      BFFramework/Classes/Stuckpoint/View/PQSelecteMusicView.swift

+ 8 - 6
BFFramework/Classes/Stuckpoint/View/PQSelecteMusicView.swift

@@ -148,6 +148,7 @@ class PQSelecteMusicView: UIView {
         let musicIndex = musicDatas.firstIndex(where: { (music) -> Bool in
             (music.musicId == model.musicId)
         })
+        
         if(musicIndex == nil){
             model.voiceStatue = .isSelected
             searchMusiceDatas.insert(model, at: 0)
@@ -156,17 +157,18 @@ class PQSelecteMusicView: UIView {
                 musicDatas.insert(model, at: 0)
             }
         }else{
-            musicDatas[musicIndex!].voiceStatue = .isSelected
+            
+            let selectMusicData = musicDatas[musicIndex!]
+            selectMusicData.voiceStatue = .isSelected
+            musicDatas.remove(at: musicIndex!)
+            musicDatas.insert(selectMusicData, at: 0)
         }
 
         selectMusicCollection.reloadData()
        
         //划动到选择的音乐位置
-        if(musicIndex != nil){
-            selectMusicCollection.scrollToItem(at: IndexPath(row: musicIndex!, section: 0), at: .centeredHorizontally, animated: true)
-        }else{
-            selectMusicCollection.scrollToItem(at: IndexPath(row: 0, section: 0), at: .centeredHorizontally, animated: true)
-        }
+        selectMusicCollection.scrollToItem(at: IndexPath(row: 0, section: 0), at: .centeredHorizontally, animated: true)
+ 
     }
     func autolayout() {
         categoryCollection.snp.makeConstraints { make in