I have a UITableView
and some UITableViewCells
which i have created manually via the Interface Builder. I've assigned each cell an outlet
, and im connecting them to the UITableView
in the CellForRowAtIndexPath
method. In this method, I use the switch(case)
method to make specific cells appear in the UITableView
, depends on the case.
Now, I want to find a specific cell and check if he is exists within the UITableView
. I use the method: UITableView.visibleCells
to get an array of the cells in the table view. My question is - how can i check if a specific cells exists in the array? can I use the outlet that i've assigned to it somehow? - (The best solution),OR, can I use an identifier and how?
Thanks :)
For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.
Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.
The visual representation of a single row in a table view. A UITableViewCell object is a specialized type of view that manages the content of a single table row. You use cells primarily to organize and present your app’s custom content, but UITableViewCell provides some specific customizations to support table-related behaviors, including:
The trick is to get the superview of the button, which will be the cell, and then using tableView.indexPathForCell (cell) to get the index path. @IBAction func tapOnButton(sender: UIButton) { let cell = sender.superview as! UITableViewCell let indexPath = tableView.indexPathForCell(cell) }
The UITableViewSource 's NumberOfSections (UITableView) and RowsInSection (UITableView, nint) methods allow the UITableView to request only the data necessary for the cells on the screen. A specific cell is identified by an NSIndexPath, whose Section and Row properties will specify a unique cell.
Every table view must have at least one type of cell for displaying content, and tables may have multiple cell types to display different types of content. Your table’s data source object handles the creation and configuration of cells immediately before they appear onscreen.
Note that you can as well use indexPathsForVisibleRows
this way:
NSUInteger index = [_people indexOfObject:person]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; if ([self.tableView.indexPathsForVisibleRows containsObject:indexPath]) { [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }
If you have the indexPath (and don't need the actual Cell) it might be cheaper.
PS: _people
is the NSArray
used as my backend in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With