Browse Source

1, 搜索出来音乐数据错乱问题 2, 选中后移动到批定位置

jsonwang 3 years ago
parent
commit
fbc4a31323
1 changed files with 22 additions and 9 deletions
  1. 22 9
      BFFramework/Classes/Stuckpoint/View/PQSelecteMusicView.swift

+ 22 - 9
BFFramework/Classes/Stuckpoint/View/PQSelecteMusicView.swift

@@ -138,19 +138,32 @@ class PQSelecteMusicView: UIView {
     //插入数据
     func insertSearchMusic(model:PQVoiceModel) {
       
-        for oldModel in searchMusiceDatas {
+        for oldModel in musicDatas {
             oldModel.voiceStatue = .isNormal
         }
-        
-        model.voiceStatue = .isSelected
-        searchMusiceDatas.insert(model, at: 0)
-        
-        //有搜索的数据
-        if(currentSelectTag?.tagId == 425){
-            musicDatas.insert(contentsOf: searchMusiceDatas, at: 0)
+   
+        let musicIndex = musicDatas.firstIndex(where: { (music) -> Bool in
+            (music.musicId == model.musicId)
+        })
+        if(musicIndex == nil){
+            model.voiceStatue = .isSelected
+            searchMusiceDatas.insert(model, at: 0)
+            //有搜索的数据
+            if(currentSelectTag?.tagId == 425){
+                musicDatas.insert(model, at: 0)
+            }
+        }else{
+            musicDatas[musicIndex!].voiceStatue = .isSelected
         }
-        
+
         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)
+        }
     }
     func autolayout() {
         categoryCollection.snp.makeConstraints { make in