2014-12-11 26 views
5

鑑於在任何給定時間單細胞可見一個UITableView,我怎麼能確定哪個小區是鑑於而表視圖被滾動?UITableView的一個可見單元:確定哪些是最明顯的

我知道我可以做這個拿到可見單元格數組:做

NSArray *paths = [tableView indexPathsForVisibleRows]; 

然後拿到最後一個單元格(或第一,或其他):

UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:[paths lastObject]]; 

但如何我比較所有可見的細胞,並確定哪些是最可見的?

回答

3

的算法不同,這取決於你有多少路回去:

  • 如果只有一條路徑,這是最明顯的單元格右側有
  • 如果有三個或更多的路徑,任何的中間的單元格(即除第一個單元格和最後一個單元格外的所有單元格)同樣可見
  • 如果確切有兩個單元格,請在父視圖中找到將兩者分隔開的線的位置,然後計算兩個距離 - 從中​​到中,從中到下。如果頂部到中部更大,那麼頂部單元格是最明顯的。如果中下部更大,則第二個單元格更加明顯。否則,這兩個單元格同樣可見。

*中點位置是第二個單元的底部。頂部和底部位置是表格視圖的頂部和底部。

+0

這實際上可能使最有意義的,因爲如果釋放觸摸時只有一個單元格可見,滾動時最多可以看到兩個單元格。 – brandonscript 2014-12-11 04:00:59

+1

終於有機會嘗試了 - 效果很好,謝謝! – brandonscript 2014-12-19 22:16:48

0

您可以使用表格視圖的rectForRowAtIndexPath:讓每個可見單元格的框架,然後抵銷(與CGRectOffset)由-contentOffset.y考慮到滾動,然後與相交他們表視圖的bounds找出每個單元格是多少在表格視圖內可見。

0

下面的邏輯將爲您提供最顯眼的UITableViewCell,或每當用戶停止滾動時立即在UITableView中居中。希望這個邏輯能夠幫助某人。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 
    if (!decelerate) 
    { 
     if (isScrollingStart) 
     { 
      isScrollingStart=NO; 
      isScrollingEnd=YES; 
      [self scrollingStopped]; 
     } 
    } 
} 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 

    if (isScrollingStart) 
    { 
     isScrollingStart=NO; 
     isScrollingEnd=YES; 
     [self scrollingStopped]; 
    } 
} 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    isScrollingStart=YES; 
} 
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    isScrollingStart=YES; 
} 
-(void)scrollingStopped 
{ 
    NSMutableArray* arrVideoCells=[NSMutableArray array]; 
    NSLog(@"Scrolling stopped"); 
    NSArray* arrVisibleCells=[self.tableTimeline visibleCells]; 
    for (TimeLineCell* cell in arrVisibleCells) 
    { 
     if ([cell isKindOfClass:[TimeLineCellMediaVideo class]]) 
     { 
      [arrVideoCells addObject:cell]; 
     } 
    } 

    TimeLineCellMediaVideo* videoCell=[self getCellNearCenterOfScreen:arrVideoCells]; 

} 
-(TimeLineCellMediaVideo*)getCellNearCenterOfScreen:(NSMutableArray*)arrCells 
{ 
    TimeLineCellMediaVideo* closetCellToCenter; 
    CGRect filterCGRect; 
    for (TimeLineCellMediaVideo* videoCell in arrCells) 
    { 
     if (arrCells.count==1) 
      closetCellToCenter= videoCell; 


     NSIndexPath* cellIndexPath=[self.tableTimeline indexPathForCell:videoCell]; 
     CGRect rect = [self.tableTimeline convertRect:[self.tableTimeline rectForRowAtIndexPath:cellIndexPath] toView:[self.tableTimeline superview]]; 
     if (closetCellToCenter) 
     { 

      CGRect intersect = CGRectIntersection(self.tableTimeline.frame, filterCGRect); 
      float visibleHeightFilterCell = CGRectGetHeight(intersect); 

      intersect = CGRectIntersection(self.tableTimeline.frame, rect); 
      float visibleHeightCurrentCell = CGRectGetHeight(intersect); 
      if (visibleHeightCurrentCell>visibleHeightFilterCell) 
      { 
       filterCGRect=rect; 
       closetCellToCenter= videoCell; 
      } 

     } 
     else 
     { 
      closetCellToCenter=videoCell; 
      filterCGRect=rect; 

     } 
    } 
    return closetCellToCenter; 
} 
3

下面的邏輯會得到您最明顯的細胞在滾動的末尾:

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

    CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.tableView.bounds.size}; 
    CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect)); 
    NSIndexPath *visibleIndexPath = [self.tableView indexPathForRowAtPoint:visiblePoint]; 

} 
1
基於

迅速解決對@ Sebyddd的回答是:

func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 
    scrollToMostVisibleCell() 
} 

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { 
    if !decelerate{ 
    scrollToMostVisibleCell() 
    } 
} 

func scrollToMostVisibleCell(){ 
    let visibleRect = CGRect(origin: tableView.contentOffset, size: tableView.bounds.size) 
    let visiblePoint = CGPoint(x: CGRectGetMidX(visibleRect), y: CGRectGetMidY(visibleRect)) 
    let visibleIndexPath: NSIndexPath = tableView.indexPathForRowAtPoint(visiblePoint)! 

    tableView.scrollToRowAtIndexPath(visibleIndexPath, atScrollPosition: .Top, animated: true) 
} 
相關問題