簡體   English   中英

滾動Swift時Tableview內容重新排序

[英]Tableview contents getting reordered when scrolled Swift

我創建了一個帶有tableview單元格的tableview。 我可以在表格視圖的不同行上列出內容(我在表格視圖的行上列出設備中的歌曲)。 下面給出了此代碼段。

override func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath ) -> UITableViewCell {

 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! hostTableViewCell    //hostTableViewCell is the custom cell class

    cell.backgroundColor = UIColor.clearColor()

    print("\(songList[indexPath.section + StaticThing.timesCalled].songTitle)")

    cell.clientCellLabel!.text = songList[indexPath.section + StaticThing.timesCalled].songTitle

    copyOfAllSongsArray.append(songList[indexPath.section + StaticThing.timesCalled].songTitle)    // Taking text of the cells in an array

    cell.detailTextLabel!.text = "\(songList[indexPath.section + StaticThing.timesCalled].artistName)"

    StaticThing.doSomething()

    if (StaticThing.timesCalled == songList.count){

        StaticThing.timesCalled = 0

    }

    return cell;

}

首次加載時的tableview顯示其內容。 但是,每次滾動頁面時,表視圖的內容都會重新排序。 每次滾動表時,內容都會以隨機方式重新排序。 如何解決此問題?

您的代碼沒有任何意義。 您正在使用一些可變的時間timesCalled來索引到您的數據數組嗎? 無法保證系統獲取cellForRowAtIndexPath單元格的順序。 您需要使用在indexPath中傳遞給您的節號和行號來確定要返回的項目,而不是使用方法被調用的次數。

您的cellForRowAtIndexPath的邏輯需要重做。 這是完全錯誤的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM