Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a specific UITableViewCell is visible in a UITableView

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 :)

like image 483
ozking Avatar asked Nov 08 '11 15:11

ozking


People also ask

How can we use a reusable cell in UITableView?

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.

What is IndexPath in Tableview Swift?

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.

What is a uitableviewcell?

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:

How to get the index path of a UITableView cell?

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) }

What is numberofsections and rowsinsection in UITableView?

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.

How many types of cells are in a table view?

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.


1 Answers

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.

like image 121
StuFF mc Avatar answered Sep 21 '22 15:09

StuFF mc