Data updates and Cell updates after TableView has been sorted

Asked 2 years ago, Updated 2 years ago, 87 views

Hello
I'm in trouble because I can't find it even if I look into it.
I would like to ask you about updating cells when sorting UITableView

 functableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath:NSIndexPath, toIndexPath destinationIndexPath:NSIndexPath){
    // Save temporarily
    let cellData=cellData [sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellData.insert (cellData, atIndex:destinationIndexPath.row)   
}

I think I'm going to replace the data source like this.
I really want to update the tableView cells after this.

 functableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath:NSIndexPath, toIndexPath destinationIndexPath:NSIndexPath){
    // Save temporarily
    let cellData=cellData [sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellData.insert (cellData, atIndex:destinationIndexPath.row)
    // Add reload
    tableView.reloadData()  
}

This will certainly update the cell data, but
≡ It is unnatural because the behavior is too fast when you grab it and let it go.So,

 functableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath:NSIndexPath, toIndexPath destinationIndexPath:NSIndexPath){
    // Save temporarily
    let cellData=cellData [sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellData.insert (cellData, atIndex:destinationIndexPath.row)
    // Reload Start
    tableView.beginUpdates()
    tableView.moveRowAtIndexPath(sourceIndexPath, toIndexPath:destinationIndexPath)
    tableView.endUpdates()
    // Reload termination
}

I tried to write that the cell will not reload successfully this time.

As for the reason why I want to reload the cell, I want to keep the current cell line number in the properties of the cell's parts.(I want to call the following again after moving を)

 functableView(tableView:UITableView, cellForRowAtIndexPath:NSIndexPath) ->UITableViewCell{
    // Set the value of the cell's parts properties.
    letcell=tableView.dueReusableCellWithIdentifier("CustomCell", forIndexPath:indexPath) as!CustomCell
    cell.view.row=indexPath.row
}

Thanks to you, it has been solved.Thank you.
I left a comment under this question, but I'm not used to the markdown, so it's hard to see.
I would like to rewrite the code alone.I'm sorry.

Place the closure on the CustomCell properties

var buttonTapAction={}

Pass the method in ViewController using the method to return the cell

 functableView(tableView:UITableView, cellForRowAtIndexPath:NSIndexPath) ->UITableViewCell{
    letcell=tableView.dueReusableCellWithIdentifier("CustomCell", forIndexPath:indexPath) as!CustomCell 
    cell.buttonTapAction={self.buttonTapAction(cell)} 
} 

Provide methods in the same ViewController (buttonTapAction(_:) here)

 func buttonTapAction(cell:TextTableViewCell){ 
    letrow=tableView.indexPathForCell(cell)?row 
    Processing with //row
}

I was able to retrieve it in a different part from .

swift ios tableview

2022-09-30 19:48

1 Answers

I am not confident that I fully understand your intentions, but if you want to delay the reload, you will have to use the Grand Central Dispath (GCD) to process the delay.

Swift3

 functableView(_tableView:UITableView, moveRowAtsourceIndexPath:IndexPath, to destinationIndexPath:IndexPath){
    let cellData=cellData [sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellData.insert (cellData, atIndex:destinationIndexPath.row)
    // Reload after 1 second delay
    DispatchQueue.main.asyncAfter(deadline:DispatchTime.now()+.seconds(1), execute: {tableView.reloadData()})  
}

Swift2

 functableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath:NSIndexPath, toIndexPath destinationIndexPath:NSIndexPath){
    let cellData=cellData [sourceIndexPath.row]
    cellDatas.removeAtIndex(sourceIndexPath.row)
    cellData.insert (cellData, atIndex:destinationIndexPath.row)
    // Reload after 1 second delay
    let delayTime = dispatch_time(DISPATCH_TIME_NOW, (NSEC_PER_SEC*1))
    dispatch_after(delayTime, dispatchQueue, {tableView.reloadData()})
}


2022-09-30 19:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.