简体   繁体   中英

UITableView rows are repeating swift when reodered

I am having a tableview and some data are populated in to the 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.

For example this is the tableView data

A

B

C

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.

Now after reordering A to the very bottom and then scrolling up and down

B

C

D

D

After searching I found preapreForReuse, but it is not working.

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.

When you move A to D you need to update the model too, not just the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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