2012-02-22 79 views
4

UITableView有確定哪些單元當前可見的方法。我試圖找出的是一個單元格是多少可見的。如何判斷有多少UITableViewCell可見

例如,當您向下拖動表格頂部的'新可見'單元格時,只會出現一個(像素)行,直到整個單元格可見爲止。如何在拖動表格視圖的任何時刻分辨該單元格有多少可見?

當用戶在桌子上拖動時,我的最終目標是根據在任何給定時間可見的單元格數量來更改單元格內的顯示視圖。

有什麼建議嗎?

回答

4

我沒有測試過,但我想嘗試沿着線的東西:

UITableViewCell *cell; 
UIView *parent = cell.superview; 
CGRect overlap = CGRectIntersection(cell.frame, parent.bounds); 

然後比較特別的矩形。

2

我用bshirley的建議和其他一些做出這樣的事情。像我的東西一樣的魅力,一次只顯示幾個單元格。可能需要一些調整,如果你想與更多的細胞

- (void) scrollViewDidEndDecelerating:(UITableView *)scrollView { 

NSArray *visibleCellIndexPaths = TableView.indexPathsForVisibleRows; 
NSIndexPath *indexPathLow = [visibleCellIndexPaths valueForKeyPath:@"@min.self"]; 
NSIndexPath *indexPathHigh = [visibleCellIndexPaths valueForKeyPath:@"@max.self"]; 

NSArray *cells = TableView.visibleCells; 

UITableViewCell *cell1 = [cells objectAtIndex:0]; 
UIView *parent1 = cell1.superview; 
CGRect overlap1 = CGRectIntersection(cell1.frame, parent1.bounds); 

UITableViewCell *cell2 = [cells objectAtIndex:1]; 
UIView *parent2 = cell2.superview; 
CGRect overlap2 = CGRectIntersection(cell2.frame, parent2.bounds); 

if (overlap1.size.height > overlap2.size.height) { 
    [TableView scrollToRowAtIndexPath:indexPathLow atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} else { 
    [TableView scrollToRowAtIndexPath:indexPathHigh atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 

}

2

在這裏使用這是斯威夫特實施上述的變型:是單元的一部分

var cellRect = tableView.rectForRowAtIndexPath(indexPath) 
    if let superview = tableView.superview { 
     let convertedRect = tableView.convertRect(cellRect, toView:superview) 
     let intersect = CGRectIntersection(tableView.frame, convertedRect) 
     let visibleHeight = CGRectGetHeight(intersect) 
    } 

visibleHeight這是可見的。一個另外的步驟可以被添加到計算的比率 - 零和一之間 - 即上可見的細胞的:

var cellRect = tableView.rectForRowAtIndexPath(indexPath) 
    if let superview = tableView.superview { 
     let convertedRect = tableView.convertRect(cellRect, toView:superview) 
     let intersect = CGRectIntersection(tableView.frame, convertedRect) 
     let visibleHeight = CGRectGetHeight(intersect) 
     let cellHeight = CGRectGetHeight(cellRect) 
     let ratio = visibleHeight/cellHeight 
    } 

要取決於能見度改變視圖外觀 - 作爲問題狀態以上 - 此代碼應被包括在表格視圖的UIScrollView超類代表,UIScrollViewDelegate方法scrollViewDidScroll

但是,這隻會影響他們滾動的單元格。已經可見的單元格不會受到影響。對於這些,UITableViewDelegate方法didEndDisplayingCell中應使用相同的代碼。

4

你可以嘗試這樣的事情:

-(void)scrollViewDidScroll:(UIScrollView *)sender 
{ 
    [self checkWhichVideoToEnable]; 
} 

-(void)checkWhichVideoToEnable 
{ 
    for(UITableViewCell *cell in [tblMessages visibleCells]) 
    { 
     if([cell isKindOfClass:[VideoMessageCell class]]) 
     { 
      NSIndexPath *indexPath = [tblMessages indexPathForCell:cell]; 
      CGRect cellRect = [tblMessages rectForRowAtIndexPath:indexPath]; 
      UIView *superview = tblMessages.superview; 

      CGRect convertedRect=[tblMessages convertRect:cellRect toView:superview]; 
      CGRect intersect = CGRectIntersection(tblMessages.frame, convertedRect); 
      float visibleHeight = CGRectGetHeight(intersect); 

      if(visibleHeight>VIDEO_CELL_SIZE*0.6) // only if 60% of the cell is visible 
      { 
       // unmute the video if we can see at least half of the cell 
       [((VideoMessageCell*)cell) muteVideo:!btnMuteVideos.selected]; 
      } 
      else 
      { 
       // mute the other video cells that are not visible 
       [((VideoMessageCell*)cell) muteVideo:YES]; 
      } 
     } 
    } 
} 
+0

感謝隊友,我還沒有想過用CGRectIntersection,現在這麼明顯! – 2017-03-10 15:07:43

+1

很高興能幫到你... – Catalin 2017-03-13 01:56:52

相關問題