I created a tableview using custom cell. I want reload the target cell, let's say [section = 0, row = 0], and use the UITableView.reloadRows method.
let indexPath = IndexPath.init(row: 0, section: 0)
self.tableView.reloadRows(at: [indexPath], with: .fade)
When the button in the target cell is tapped, this method is called.
When this method called, the target cell [section = 0, row = 0] is not reloaded, but the tableview is scrolled and other cells [(section = 0, row = 2),(section = 0, row = 3)] are reloaded.
I have no idea why this happens.
Please help me if anyone has any solutions.
Best regards,
I should have been more precise. Reloading is for all, only redisplay is partial. Sorry for creating confusion.
Instance Method reloadData()
Reloads the rows and sections of the table view.
Declaration
func reloadData()
Discussion
Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view’s delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to
beginUpdates()
and endUpdates()
.To change the height when you tap the button in cell:
- first, get the cell that contains the button (I copy from an existing project, did not test in this context, but should work)
One way to do this is to explore all cells (in the button IBAction):
if let cell = sender.superview?.superview {
if let theTableView = cell.superview as? UITableView { // So, the button was in a cellView
for row in 0..<thetableview.numberofrows(insection: 0)="" { // I assume there is a single section
let indexPath = IndexPath(row: row, section: 0)
if let cellAtRow = theTableView.cellForRow(at: indexPath) { // Will call didSelectRowAt
theTableView.selectRow(at: indexPath, animated: true, scrollPosition: .none) // Could scroll to position
AndI tested by modifying slightly the code
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var aTableView: UITableView!
var isTapped: Bool = false
var selectedCellPath: IndexPath? // NEW
override func viewDidLoad() {
super.viewDidLoad()
aTableView.delegate = self
aTableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
print("heightForRowAt", indexPath, selectedCellPath)
if selectedCellPath != nil && indexPath.row == selectedCellPath!.row { // 0 { // CHANGED
return 100.0
}
return 50.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel!.text = "cell \(indexPath.row)"
let backgroundView = UIView()
backgroundView.backgroundColor = .red
cell.selectedBackgroundView = backgroundView
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt:", indexPath)
isTapped = true
selectedCellPath = indexPath
tableView.reloadRows(at: [indexPath], with: .none)
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .middle)
}
}