繁体   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