简体   繁体   中英

iPhone dev - How can I find out at runtime what the top cell that's currently being displayed is in a UITableView?

Is there any way to know which cells are currently being displayed? I'm guessing there might be some type of array property that would store the cells currently on the screen, with the top-most cells having the lowest indexes in the array, or something along those lines. Is there anything like this out there? If not, how could I go about figuring this out?

visibleCells and indexPathsForVisibleRows is what you are looking for.

  • visibleCells returns an array of UITableViewCell objects.

  • indexPathForVisibleRows returns an array of NSIndexPath objects.

You can use -[UITableView indexPathsForVisibleRows] , which returns an NSArray of NSIndexPaths for all visible rows. If you need to get access to the actual cell you can use -[UITableView cellForRowAtIndexPath:] on an individual NSIndexPath, or you can use -[UITableView visibleCells] to get an array of the actual cells themselves.

In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath , try to append indexPath to the content you're displaying?

So instead of cell = something; , do cell = [NSString stringWithFormat:@"%@ - %d", something, indexPath];

UITableViewCell *thisCell = (UITableViewCell*)[tableView cellForRowAtIndexPath:[myArray objectAtIndex:0]];

Then, thisCell is an actual cell, so you can call stuff like thisCell.textLabel.text , or any other properties of cells.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM