ios - 滚动 Swift 时 Tableview 内容会重新排序

标签 ios swift uitableview scroll

我创建了一个表格 View ,其中包含表格 View 单元格。我能够在表格 View 的不同行上列出内容(我在表格 View 的行上列出设备中的歌曲)。下面给出了相关的代码片段。

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;

}

第一次加载时的表格 View 显示其内容。但是,当每次滚动页面时, TableView 的内容都会重新排序。每次滚动表格时,内容都会以随机方式重新排序。如何解决这个问题?

最佳答案

你的代码没有任何意义。您正在使用一些变量 timesCalled 来索引数据数组吗?系统在 cellForRowAtIndexPath 中获取单元格的顺序无法保证。您需要使用在indexPath 中传递给您的节号和行号来确定要返回哪个项目,而不是使用调用该方法的次数。

需要重做 cellForRowAtIndexPath 的逻辑。这是完全错误的。

关于ios - 滚动 Swift 时 Tableview 内容会重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025114/

相关文章:

ios - 如何为 epub 阅读器 webview 设置背景图像(纹理)?

ios - 在 Swift 中删除 TabBar 项目

uitableview - scrollToRowAtIndexPath 在 iOS 7 中停止工作 - ?UITableViewCellScrollView?更改 UITableViewCell 层次结构

ios - 为扩展 View 添加委托(delegate)和数据源

ios - 如何在 Swift 中循环函数?

objective-c - UITableViewCell awakeFromNib 帧大小错误

ios - UITableView 中包含 UIView 的滚动问题

ios - NSPredicate 和丹麦字母 å 给出了错误的结果

ios - 核心数据导入 - 不释放内存

swift - 在编程 collectionviewCell 中添加约束