Last active
December 20, 2019 08:20
-
-
Save alizainprasla/4ae389742cb3356e05754c73bc1a0c4d to your computer and use it in GitHub Desktop.
TableView reload cell while editing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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