2016-09-29 63 views
0

對不起,我知道這是一個重複問題,但我無法理解其他問題的答案。斯威夫特3滾動自定義單元格更改

我的自定義單元正在更改滾動。我知道這是由於重用標識符,但我不知道如何解決它。任何幫助,將不勝感激。我也使用indexPath.row來訪問數組。

如何讓我的表視圖不會更改滾動值,即使通過使用重用標識符?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell : TeamCell = tableView.dequeueReusableCell(withIdentifier: "TeamCell", for: indexPath) as! TeamCell 

    cell.trainer.image = UIImage(named: "t\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].imageID)") 
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p1.name != 0 { 
     cell.pokemon1.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p1.name)") 
     cell.pokemon1.isHidden = false 
    } else { 
     cell.pokemon1.isHidden = true 
    } 
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p2.name != 0 { 
     cell.pokemon2.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p2.name)") 
     cell.pokemon1.isHidden = false 
    } else { 
     cell.pokemon2.isHidden = true 
    } 
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p3.name != 0 { 
     cell.pokemon3.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p3.name)") 
     cell.pokemon1.isHidden = false 
    } else { 
     cell.pokemon3.isHidden = true 
    } 
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p4.name != 0 { 
     cell.pokemon4.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p4.name)") 
     cell.pokemon1.isHidden = false 
    } else { 
     cell.pokemon4.isHidden = true 
    } 
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p5.name != 0 { 
     cell.pokemon5.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p5.name)") 
     cell.pokemon1.isHidden = false 
    } else { 
     cell.pokemon5.isHidden = true 
    } 
    if Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p6.name != 0 { 
     cell.pokemon6.image = UIImage(named: "\(Player.User.Gold.trainers[(indexPath as NSIndexPath).row].p6.name)") 
     cell.pokemon1.isHidden = false 
    } else { 
     cell.pokemon6.isHidden = true 
    } 

    return cell 
} 

(功能只是檢查是否數組的值是0,如果不是則顯示出圖像)

+0

你爲什麼在每一個設置pok1 if hidden = flase?不應該是你設置圖像的口袋妖怪? –

+0

我犯了一個菜鳥錯誤,沒有改變if語句中的正確值! Whoopsie – mint

+0

是的,修復它!我的臉是紅的,謝謝aha – mint

回答

1

所以就像評論說,這個問題是每個人如果錯誤地設置pokemon1。

編輯

很好,因爲downvote我會更加precice的: 在每個如果條件U設置pokemon1到vissible。您需要使用Pokomen將您的設置作爲圖像。

玩得開心編碼

1

設置你的每一個寵物小精靈imagesnildequeue後,一些常見的placeholder image立即該單元格,然後把你的每個if-conditions並設置每個圖像視圖的默認隱藏屬性,如果你的圖像視圖默認隱藏,然後將其隱藏屬性設置爲true,然後設置圖像在if條件!

+0

好吧,這只是一個與他不同的實現。我認爲這個問題是錯誤的小寵物設置每一個如果 –

+0

Tableview重新使用單元格,所以如果對於特定的單元格,如果沒有圖像可用,那麼它顯示已經爲以前的單元格設置的舊圖像!因此使它成爲零,每次都會使imageview的圖像成爲零,如果沒有可用於單元格的圖像,則它將保留爲零或某個佔位符圖像! – Lion

+0

但他將其設置爲hidden = false –