ios – 如何检索所有可见的表部分标题视图

前端之家收集整理的这篇文章主要介绍了ios – 如何检索所有可见的表部分标题视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法获取可见的所有部分标题视图?

类似于UITableView的visibleCells实例方法

解决方法

使用indexPathsForVisibleRows的问题是它不包括没有任何行的节.要获取所有可见的部分,包括空白部分,您必须检查该部分的rect,并将其与表的contentOffset进行比较.

您还必须注意浮动部分的平面样式与没有浮动部分的分组样式之间的区别.

我做了一个支持这个计算的类别:

  1. @interface UITableView (VisibleSections)
  2.  
  3. // Returns an array of NSNumbers of the current visible section indexes
  4. - (NSArray *)indexesOfVisibleSections;
  5. // Returns an array of UITableViewHeaderFooterView objects of the current visible section headers
  6. - (NSArray *)visibleSections;
  7.  
  8. @end
  9.  
  10. @implementation UITableView (VisibleSections)
  11.  
  12. - (NSArray *)indexesOfVisibleSections {
  13. // Note: We can't just use indexPathsForVisibleRows,since it won't return index paths for empty sections.
  14. NSMutableArray *visibleSectionIndexes = [NSMutableArray arrayWithCapacity:self.numberOfSections];
  15. for (int i = 0; i < self.numberOfSections; i++) {
  16. CGRect headerRect;
  17. // In plain style,the section headers are floating on the top,so the section header is visible if any part of the section's rect is still visible.
  18. // In grouped style,the section headers are not floating,so the section header is only visible if it's actualy rect is visible.
  19. if (self.style == UITableViewStylePlain) {
  20. headerRect = [self rectForSection:i];
  21. } else {
  22. headerRect = [self rectForHeaderInSection:i];
  23. }
  24. // The "visible part" of the tableView is based on the content offset and the tableView's size.
  25. CGRect visiblePartOfTableView = CGRectMake(self.contentOffset.x,self.contentOffset.y,self.bounds.size.width,self.bounds.size.height);
  26. if (CGRectIntersectsRect(visiblePartOfTableView,headerRect)) {
  27. [visibleSectionIndexes addObject:@(i)];
  28. }
  29. }
  30. return visibleSectionIndexes;
  31. }
  32.  
  33. - (NSArray *)visibleSections {
  34. NSMutableArray *visibleSects = [NSMutableArray arrayWithCapacity:self.numberOfSections];
  35. for (NSNumber *sectionIndex in self.indexesOfVisibleSections) {
  36. UITableViewHeaderFooterView *sectionHeader = [self headerViewForSection:sectionIndex.intValue];
  37. [visibleSects addObject:sectionHeader];
  38. }
  39.  
  40. return visibleSects;
  41. }
  42.  
  43. @end

猜你在找的iOS相关文章