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

时间:2016-05-04 10:29:33

标签: ios swift uitableview scroll

我创建了一个包含tableview单元格的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显示其内容。但是,每次滚动页面时,tableview的内容都会重新排序。每次滚动表时,内容都会以随机方式重新排序。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

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

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