UITableView 的 indexPathsForVisibleRows 更新除一个单元格之外的所有单元格
UITableView's indexPathsForVisibleRows updates all cells except one
我有一个 UITableView,它使用 NSFetchedResultsController 填充它的单元格。我还使用 indexPathsForVisibleRows 来更新可见单元格,但我点击和编辑的单元格除外。但是 UI 使用正确的计算更新所有单元格,除了一个。如果我滚动该 tableView,那么该单元格会在下次可见时重新计算。
有问题的GIF:CLICK
我定义了要编辑的单元格,并为除我正在编辑的单元格之外的所有单元格重新加载行:
func textFieldDidChangeSelection(_ textField: UITextField) {
let tapLocation = textField.convert(textField.bounds.origin, to: tableView)
guard let indexPath = tableView.indexPathForRow(at: tapLocation) else { return }
pickedCurrency = fetchedResultsController.object(at: indexPath)
let visibleIndexPath = tableView.indexPathsForVisibleRows ?? []
var nonActiveIndexPaths = [IndexPath]()
for index in visibleIndexPath where index != indexPath {
nonActiveIndexPaths.append(index)
}
tableView.reloadRows(at: nonActiveIndexPaths, with: .none)
}
为什么 UI 更新除一个单元格之外的所有单元格?找不到原因...
以下是我设法解决问题的方法。我理解的是,我应该避免在我的案例中使用 tableView.indexPathsForVisibleRows
,因为正如@ShawnFrank 所说,它只会重新加载可见的单元格:
func textFieldDidChangeSelection(_ textField: UITextField) {
//Code for defining an active cell (on which I clicked to edit its textField)
let tapLocation = textField.convert(textField.bounds.origin, to: tableView)
guard let pickedCurrencyIndexPath = tableView.indexPathForRow(at: tapLocation) else { return }
pickedCurrency = fetchedResultsController.object(at: pickedCurrencyIndexPath)
//Array for all IndexPaths which is not selected, i.e. not active
var nonActiveIndexPaths = [IndexPath]()
//Define all rows tableView has at the moment for particular section
//In my case it can be only 1 section which starts at 0 index
let tableViewRows = tableView.numberOfRows(inSection: 0)
for i in 0..<tableViewRows {
//Create indexPath with the rows and section
let indexPath = IndexPath(row: i, section: 0)
//Add all IndexPaths to previously created array, except the one that is active now
if indexPath != pickedCurrencyIndexPath {
nonActiveIndexPaths.append(indexPath)
}
}
//Reload only rows from the array
tableView.reloadRows(at: nonActiveIndexPaths, with: .none)
}
我有一个 UITableView,它使用 NSFetchedResultsController 填充它的单元格。我还使用 indexPathsForVisibleRows 来更新可见单元格,但我点击和编辑的单元格除外。但是 UI 使用正确的计算更新所有单元格,除了一个。如果我滚动该 tableView,那么该单元格会在下次可见时重新计算。
有问题的GIF:CLICK
我定义了要编辑的单元格,并为除我正在编辑的单元格之外的所有单元格重新加载行:
func textFieldDidChangeSelection(_ textField: UITextField) {
let tapLocation = textField.convert(textField.bounds.origin, to: tableView)
guard let indexPath = tableView.indexPathForRow(at: tapLocation) else { return }
pickedCurrency = fetchedResultsController.object(at: indexPath)
let visibleIndexPath = tableView.indexPathsForVisibleRows ?? []
var nonActiveIndexPaths = [IndexPath]()
for index in visibleIndexPath where index != indexPath {
nonActiveIndexPaths.append(index)
}
tableView.reloadRows(at: nonActiveIndexPaths, with: .none)
}
为什么 UI 更新除一个单元格之外的所有单元格?找不到原因...
以下是我设法解决问题的方法。我理解的是,我应该避免在我的案例中使用 tableView.indexPathsForVisibleRows
,因为正如@ShawnFrank 所说,它只会重新加载可见的单元格:
func textFieldDidChangeSelection(_ textField: UITextField) {
//Code for defining an active cell (on which I clicked to edit its textField)
let tapLocation = textField.convert(textField.bounds.origin, to: tableView)
guard let pickedCurrencyIndexPath = tableView.indexPathForRow(at: tapLocation) else { return }
pickedCurrency = fetchedResultsController.object(at: pickedCurrencyIndexPath)
//Array for all IndexPaths which is not selected, i.e. not active
var nonActiveIndexPaths = [IndexPath]()
//Define all rows tableView has at the moment for particular section
//In my case it can be only 1 section which starts at 0 index
let tableViewRows = tableView.numberOfRows(inSection: 0)
for i in 0..<tableViewRows {
//Create indexPath with the rows and section
let indexPath = IndexPath(row: i, section: 0)
//Add all IndexPaths to previously created array, except the one that is active now
if indexPath != pickedCurrencyIndexPath {
nonActiveIndexPaths.append(indexPath)
}
}
//Reload only rows from the array
tableView.reloadRows(at: nonActiveIndexPaths, with: .none)
}