2016-02-14 44 views
0

我正在製作一個簡單的桌面視圖應用來顯示和播放所有iOS系統的聲音。如何讓表格視圖一次加載所有數據,而不是在滾動時更改所述數據?

我以[ID(Int):Name(String)]的形式在aa字典中擁有所有的聲音和ID(我現在認識到這是一種不好的方式來做到這一點),問題是當我加載我的視圖加載好,但如果我向下滾動最上面的單元格改變。從底部滾動到頂部時相同。

例如,在視圖中的負載,我可以單擊並從任何我點擊的細胞的聽到各種聲音。假設第一個單元格是「SMS-Sound1」,秒數是「SMS-Sound2」。現在,當我向下滾動到那些單元格不在視圖中的位置,然後回滾到頂部時,它們被命名爲不同的(仍然來自我的數據字典)。

我怎麼會這樣,它加載的tableview,然後實現代碼如下數據不會改變解決這個問題?

編輯:我認爲這個問題可能是因爲in循環被執行了大約300,000次,但事實並非如此,它創建了一個IDS數組,因此它只執行了大約1000次,問題依然存在

我的代碼:

小區設立

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("soundCell", forIndexPath: indexPath) 
     let button = cell.viewWithTag(3) as! UILabel //UILabel in "SoundCell" 
     for i: Int in 999..<4100 { 
      //Lowest id sound is 1000, highest is 4095 
      if (sounds[i] != nil) && loadedSoundStrings.contains(sounds[i]!) == false { 
       button.text = sounds[i] 
       loadedSoundStrings.append(sounds[i]!) 
       cell.tag = i 
       break 
      } 
     } 

     return cell 
    } 

行/段

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return sounds.count 
    } 

變量:

let sounds = 
     [ 1000:"new-mail.caf", 
      1001:"mail-sent.caf", 
      1002:"Voicemail.caf", 
      1003:"ReceivedMessage.caf", 
      1004:"SentMessag.caf", 
      1005:"alarm.caf", 
      1006:"low-power.caf", 
      1007:"sms-received1.caf", 
      1008:"sms-received2.caf", 
      1009:"sms-received3.caf", 
      1010:"sms-received4.caf", 
      1011:"-(SMSReceived_Vibrate)", 
      1012:"sms-received1.caf", 
      1013:"sms-received5.caf", 
      1014:"sms-received6.caf", 
      1015:"Voicemail.caf", 
      1016:"tweet_sent.caf", 
      1020:"Anticipate.caf", 
      1021:"Bloom.caf", 
      1022:"Calypso.caf", 
      1023:"Choo_Choo.caf", 
      1024:"Descent.caf", 
      1025:"Fanfare.caf", 
      1026:"Ladder.caf", 
      1027:"Minuet.caf", 
      1028:"News_Flash.caf", 
      1029:"Noir.caf", 
      1030:"Sherwood_Forest.caf", 
      1031:"Spell.caf", 
      1032:"Suspense.caf", 
      1033:"Telegraph.caf", 
      1034:"Tiptoes.caf", 
      1035:"Typewriters.caf", 
      1036:"Update.caf", 
      1050:"ussd.caf", 
      1051:"SIMToolkitCallDropped.caf", 
      1052:"SIMToolkitGeneralBeep.caf", 
      1053:"SIMToolkitNegativeACK.caf", 
      1054:"SIMToolkitPositiveACK.caf", 
      1055:"SIMToolkitSMS.caf", 
      1057:"Tink.caf", 
      1070:"ct-busy.caf", 
      1071:"ct-congestion.caf", 
      1072:"ct-path-ack.caf", 
      1073:"ct-error.caf", 
      1074:"ct-call-waiting.caf", 
      1075:"ct-keytone2.caf", 
      1100:"lock.caf", 
      1101:"unlock.caf", 
      1102:"-(FailedUnlock)", 
      1103:"Tink.caf", 
      1104:"Tock.caf", 
      1105:"Tock.caf", 
      1106:"beep-beep.caf", 
      1107:"RingerChanged.caf", 
      1108:"photoShutter.caf", 
      1109:"shake.caf", 
      1110:"jbl_begin.caf", 
      1111:"jbl_confirm.caf", 
      1112:"jbl_cancel.caf", 
      1113:"begin_record.caf", 
      1114:"end_record.caf", 
      1115:"jbl_ambiguous.caf", 
      1116:"jbl_no_match.caf", 
      1117:"begin_video_record.caf", 
      1118:"end_video_record.caf", 
      1150:"vc~invitation-accepted.caf", 
      1151:"vc~ringing.caf", 
      1152:"vc~ended.caf", 
      1153:"ct-call-waiting.caf", 
      1154:"vc~ringing.caf", 
      1200:"dtmf-0.caf", 
      1201:"dtmf-1.caf", 
      1202:"dtmf-2.caf", 
      1203:"dtmf-3.caf", 
      1204:"dtmf-4.caf", 
      1205:"dtmf-5.caf", 
      1206:"dtmf-6.caf", 
      1207:"dtmf-7.caf", 
      1208:"dtmf-8.caf", 
      1209:"dtmf-9.caf", 
      1210:"dtmf-star.caf", 
      1211:"dtmf-pound.caf", 
      1254:"long_low_short_high.caf", 
      1255:"short_double_high.caf", 
      1256:"short_low_high.caf", 
      1257:"short_double_low.caf", 
      1258:"short_double_low.caf", 
      1259:"middle_9_short_double_low.caf", 
      1300:"Voicemail.caf", 
      1301:"ReceivedMessage.caf", 
      1302:"new-mail.caf", 
      1303:"mail-sent.caf", 
      1304:"alarm.caf", 
      1305:"lock.caf", 
      1306:"Tock.caf", 
      1307:"sms-received1.caf", 
      1308:"sms-received2.caf", 
      1309:"sms-received3.caf", 
      1310:"sms-received4.caf", 
      1311:"-(SMSReceived_Vibrate)", 
      1312:"sms-received1.caf", 
      1313:"sms-received5.caf", 
      1314:"sms-received6.caf", 
      1315:"Voicemail.caf", 
      1320:"Anticipate.caf", 
      1321:"Bloom.caf", 
      1322:"Calypso.caf", 
      1323:"Choo_Choo.caf", 
      1324:"Descent.caf", 
      1325:"Fanfare.caf", 
      1326:"Ladder.caf", 
      1327:"Minuet.caf", 
      1328:"News_Flash.caf", 
      1329:"Noir.caf", 
      1330:"Sherwood_Forest.caf", 
      1331:"Spell.caf", 
      1332:"Suspense.caf", 
      1333:"Telegraph.caf", 
      1334:"Tiptoes.caf", 
      1335:"Typewriters.caf", 
      1336:"Update.caf", 
      1350:"-(RingerVibeChanged)", 
      1351:"-(SilentVibeChanged)", 
      4095:"-(Vibrate)"] 

var loadedSoundStrings = [String]() 
+0

問題很簡單,你的'cellForRowAtIndexPath'不考慮什麼行this _is_。這太瘋狂了。 – matt

回答

0

您實例化,所有的聲音的每一行,當你真的想只實例化的聲音已加載的行。要解決訂單問題,改變你的 cellForRowAtIdexPath

這樣:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("soundCell", forIndexPath: indexPath) 
    let button = cell.viewWithTag(3) as! UILabel //UILabel in "SoundCell" 
    button.text = sounds[i] 
    cell.tag = indexPath.row 
    return cell 
} 

這給了你,因爲你已經NumberOfRowsInSection設置爲sounds.count爲行單元格將要求每每單元1周的聲音聽起來你有。

+1

謝謝,這工作。我認爲對我來說總的來說,使用字典以外的東西會更好.. – Arch

+0

真棒你會介意接受答案。謝謝 –

0

如果我正確理解你的代碼,你要去了解它的錯誤的方式。你有一個加載一次的聲音字典。 cellForRowAtIndexPath函數應該返回一個tableViewCell和一個聲音的細節。

UITableView自動丟棄是關閉屏幕以節省內存,並會重用他們新的可見細胞。這就是您撥打dequeueReusableCellWithIdentifier的原因。因此,你應該僅僅是這樣做的:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("soundCell", forIndexPath: indexPath) 
     let button = cell.viewWithTag(3) as! UILabel //UILabel in "SoundCell" 
     //Lowest id sound is 1000, highest is 4095 
     let i = indexPath.row + 1000 
     button.text = sounds[i] 
     cell.tag = i 
     return cell 
    } 

既然你硬編碼我也做了同樣的聲音號範圍。

0

表視圖效果最佳的陣列,作爲一個陣列具有定義的順序和可以快速訪問給定元素;在cellForRowAtIndexPath中的for循環很少是一件好事。

你有一對夫婦的問題,但是,由於你的聲音標識符不從0開始,你不能使用標識符作爲直接索引到陣列,而且標識符不連續的,所以你甚至不能使用簡單的偏移量(向行號添加常數值)。

我認爲最好的辦法是不要直接依靠內在的類型,你是你的字典,而是爲每一個聲音的結構,並將它們存儲陣列。事情是這樣的:

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDatasource 

    struct Sound { 
    var id:Int 
    var fileName:String 
    } 

    var sounds=[Sound]() 

    func loadSounds() { 
     let soundsDict = 
     [1000:"new-mail.caf", 
     1001:"mail-sent.caf", 
     1002:"Voicemail.caf", 
     1003:"ReceivedMessage.caf", 
     1004:"SentMessag.caf", 
     1005:"alarm.caf", 
     1006:"low-power.caf", 
     1007:"sms-received1.caf", 
     1008:"sms-received2.caf", 
     ... 
     ] 

     for (id,fileName) in soundsDict { 
      self.sounds.append(Sound(id: id, fileName: fileName)) 
     } 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("soundCell", forIndexPath: indexPath) 
     let button = cell.viewWithTag(3) as! UILabel //UILabel in "SoundCell" 

     button.text=self.sounds[indexPath.row].fileName 

     return cell 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.sounds.count 
    }  
} 
相關問題