简体   繁体   English

滚动Swift时Tableview内容重新排序

[英]Tableview contents getting reordered when scrolled Swift

I have created a tableview with tableview cells in it. 我创建了一个带有tableview单元格的tableview。 I am able to list the contents on the different rows of the table view (I am listing the songs from device on the rows of tableview). 我可以在表格视图的不同行上列出内容(我在表格视图的行上列出设备中的歌曲)。 The code snippet for this is given below. 下面给出了此代码段。

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;

}

The tableview upon loading for the first time is showing its contents. 首次加载时的tableview显示其内容。 However, when the page is scrolled every time, the contents of the tableview gets reordered. 但是,每次滚动页面时,表视图的内容都会重新排序。 Every time the table is scrolled, the content gets reordered in a random way. 每次滚动表时,内容都会以随机方式重新排序。 How to fix this issue?. 如何解决此问题?

Your code makes no sense. 您的代码没有任何意义。 You're using some variable timesCalled to index into your data array? 您正在使用一些可变的时间timesCalled来索引到您的数据数组吗? The order in which the system fetches cells in cellForRowAtIndexPath is not guaranteed. 无法保证系统获取cellForRowAtIndexPath单元格的顺序。 You need to use the section and row numbers passed to you in the indexPath in order to figure out which item to return, not use the number of times the method is called. 您需要使用在indexPath中传递给您的节号和行号来确定要返回的项目,而不是使用方法被调用的次数。

The logic for your cellForRowAtIndexPath needs to be redone. 您的cellForRowAtIndexPath的逻辑需要重做。 It's completely wrong. 这是完全错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM