Skip to content

Instantly share code, notes, and snippets.

@alizainprasla
Last active December 20, 2019 08:20
Show Gist options
  • Save alizainprasla/4ae389742cb3356e05754c73bc1a0c4d to your computer and use it in GitHub Desktop.
Save alizainprasla/4ae389742cb3356e05754c73bc1a0c4d to your computer and use it in GitHub Desktop.
TableView reload cell while editing
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView:UITableView!
var data = Array.init(0...100)
var selectedData = [IndexPath]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.allowsMultipleSelectionDuringEditing = true
}
@IBAction func setEditing(){
let editing = tableView.isEditing
self.tableView.setEditing(!editing, animated: true)
}
@IBAction func reloadSelectedRow(){
self.tableView.beginUpdates()
self.tableView.reloadRows(at: selectedData, with: .automatic)
self.tableView.endUpdates()
for item in selectedData {
self.tableView.selectRow(at: item, animated: false, scrollPosition: .none)
}
}
}
extension ViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedData.append(indexPath)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
self.selectedData.removeAll(where: {$0 == indexPath})
}
}
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let text = data[indexPath.row]
let cell = UITableViewCell()
cell.textLabel?.text = "\(text) - Hello\(indexPath.row)"
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment