我创建了一个表格 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/