简体   繁体   English

UITableView行在重新编排时重复快速

[英]UITableView rows are repeating swift when reodered

I am having a tableview and some data are populated in to the tableview. 我有一个tableview,一些数据填充到tableview中。 Now I have a edit option and by clicking on that the user can edit the position of the row by dragging it up or down. 现在,我有了一个编辑选项,然后单击该选项,用户可以通过上下拖动来编辑行的位置。

For reordering purpose I am using the delegate functions of the tableView. 为了重新排序,我使用tableView的委托函数。

For example this is the tableView data 例如,这是tableView数据

A 一种

B

C C

D d

Now the problem is that when I click on the edit button and drag the top most cell to the bottom and place it as the last cell, and then scroll the tableView up and down(still in the editing mode) the cell at the bottom (up to which the tableView is seen currently) is repeated. 现在的问题是,当我单击编辑按钮并将最上方的单元格拖到底部并将其放置为最后一个单元格时,然后上下滚动tableView(仍处于编辑模式)底部的单元格(直到当前可以看到tableView为止)。

Now after reordering A to the very bottom and then scrolling up and down 现在,在将A重新排序到最底端然后向上和向下滚动之后

B

C C

D d

D d

After searching I found preapreForReuse, but it is not working. 搜索后,我发现preapreForReuse,但是它不起作用。

I am using custom cell class to render the table cell. 我正在使用自定义单元格类来呈现表格单元格。

class customCell: UITableViewCell {    
init(frame: CGRect) {
    super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    #add the details
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}

EDIT: 编辑:

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    var sdata = self.data[sourceIndexPath.row]
        self.data.removeAtIndex(sourceIndexPath.row)
        self.data.insert(sdata, atIndex: destinationIndexPath.row)
}

It seems very common problem but its not working for me 似乎很常见的问题,但对我不起作用

Hope you understand the problem. 希望您理解问题。

Thanks in advance. 提前致谢。

Did you change your model to reflect the change? 您是否更改了模型以反映更改? If your model has D in index 3, then that is what you will display there. 如果您的模型在索引3中具有D,则将在此处显示。

When you move A to D you need to update the model too, not just the table. 将A移至D时,您还需要更新模型,而不仅仅是表。

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

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