Determine if a tableview cell is visible

前端 未结 8 1309
野性不改
野性不改 2020-12-02 13:13

Is there any way to know if a tableview cell is currently visible? I have a tableview whose first cell(0) is a uisearchbar. If a search is not active, then hide cell 0 via a

8条回答
  •  长情又很酷
    2020-12-02 13:29

    UITableView has an instance method called indexPathsForVisibleRows that will return an NSArray of NSIndexPath objects for each row in the table which are currently visible. You could check this method with whatever frequency you need to and check for the proper row. For instance, if tableView is a reference to your table, the following method would tell you whether or not row 0 is on screen:

    -(BOOL)isRowZeroVisible {
      NSArray *indexes = [tableView indexPathsForVisibleRows];
      for (NSIndexPath *index in indexes) {
        if (index.row == 0) {
          return YES;
        }
      }
    
      return NO;
    }
    

    Because the UITableView method returns the NSIndexPath, you can just as easily extend this to look for sections, or row/section combinations.

    This is more useful to you than the visibleCells method, which returns an array of table cell objects. Table cell objects get recycled, so in large tables they will ultimately have no simple correlation back to your data source.

提交回复
热议问题