查过文档,官方提供了– visibleCells 方法来获取全部可见的cell,可是仅限于获取当前能看到的,那些须要scroll才能看到的cell获取不到。
因而想到本身写:数组
复制代码
|
可是问题仍是存在,不在屏幕范围内的cell仍是获取不到:好比说tableview中有10个cell,满屏能够显示5个。当loop到第六个cell的时候,得到是一个niloop
1.UITableViews存储他们的NSIndexPath。所以存在对部分没有对象。使用下面的代码就能够遍历表并执行的可见部分索引(我不知道你为何想要看到的部分,由于他们看到,目前在屏幕上,但无论)。 for (NSIndexPath* i in [yourTableViewName indexPathsForVisibleRows]){ NSUInteger sectionPath = [i indexAtPosition:0]; //custom code here, will run multiple times per section for each visible row in the group}2. 或者很是简单的方法是采起valueForKeyPath和的NSSet类的优点: NSSet *visibleSections = [NSSet setWithArray:[[self.tableView indexPathsForVisibleRows] valueForKey:@"section"]];基本上,你在可见的行的部分值的数组,而后填入一组与此删除重复。 3. 我已经获得了解决。 第一步,每一个部分会显示,建立一个UIView- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section,将被存储到数组中。 当TableView中滚动,我想免费的无形剖面图,因此我须要知道哪些部分是可见或不可见,请按功能代码会检测这个目的,若是视图是可见的,而后释放它。 -(BOOL)isVisibleRect:(CGRect)rect containerView:(UIScrollView*)containerView{ CGPoint point = containerView.contentOffset; CGFloat zy = point.y ; CGFloat py = rect.origin.y + rect.size.height; if (py - zy <0) { return FALSE; } CGRect screenRect = containerView.frame; CGFloat by = screenRect.size.height + zy ; if (rect.origin.y > by) { return FALSE; } return TRUE;}(rect是该部分的UIView;containerView是UITableView) 经过这种方式,我能够获得的可见部分UITableView,但我但愿在SDK能够用于此目的直接提供的API。 4. 从可见的行列表中提取部分: NSArray *indexPathsForVisibleRows = [tableView indexPathsForVisibleRows];NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];for ( NSIndexPath *indexPath in indexPathsForVisibleRows ) { [indexSet addIndex:indexPath.section];}NSLog(@"indexSet %@",indexSet);// indexSet <NSMutableIndexSet: 0x11a5c190>[number of indexes: 5 (in 1 ranges), indexes: (9-13)]或: NSArray *indexPathsForVisibleRows = [detailTableView indexPathsForVisibleRows];NSMutableSet *sectionSet = [NSMutableSet set];for ( NSIndexPath *indexPath in indexPathsForVisibleRows ) { [sectionSet addObject:[NSNumber numberWithInt:indexPath.section]];}NSLog(@"sectionSet %@",sectionSet);// sectionSet {(13, 11, 9, 10, 12 )}5. 另外一种解决方案,可使用在你的节头视图中的标签1位,这样的 #define _TBL_TAG_SECTION(_TAG) ((_TAG)|(1<<30))#define _TBL_TAG_CLEAR(_TAG) ((_TAG)&((1<<30)-1))#define _TBL_TAG_IS_SECTION(_TAG) ((_TAG)>>30)- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ // alloc header view UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; header.tag = _TBL_TAG_SECTION(section); return header;}- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGRect r = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, CGRectGetWidth(scrollView.frame), CGRectGetHeight(scrollView.frame)); for (UIView *v in [_tableView subviews]) { if ( CGRectIntersectsRect(r, v.frame) ) { if ( _TBL_TAG_IS_SECTION(v.tag) ) { NSLog(@"visible section tag %d", _TBL_TAG_CLEAR(v.tag)); } } }}6. 2步解决方案,以得到在一个UITableView可见部分: 1)添加标题视图一个可变数组viewForHeaderInSection2)更新数组时,在滚动的tableviewscrollViewDidScroll注Tag属性来保存部分的数量 @property (nonatomic, strong, readwrite) NSMutableArray *headerArray;- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)]; headerView.backgroundColor = [UIColor greenColor]; headerView.tag = section; [_headerArray addObject:headerView]; return headerView;}- (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self updateHeaderArray]; NSLog(@"------------"); for (UIView *view in _headerArray) { NSLog(@"visible section:%d", view.tag); }}- (void)updateHeaderArray { // remove invisible section headers NSMutableArray *removeArray = [NSMutableArray array]; CGRect containerRect = CGRectMake(_tableView.contentOffset.x, _tableView.contentOffset.y, _tableView.frame.size.width, _tableView.frame.size.height); for (UIView *header in _headerArray) { if (!CGRectIntersectsRect(header.frame, containerRect)) { [removeArray addObject:header]; } } [_headerArray removeObjectsInArray:removeArray];}7. 答案是简单了不少,并用简洁KVC NSArray *visibleSections = [self.tableView.indexPathsForVisibleRows valueForKey:@"section"];这可能给你重复的值的数组,但你能够从那里管理。