Ios 如何判断UITableViewCell中有多少是可见的

Ios 如何判断UITableViewCell中有多少是可见的,ios,uitableview,Ios,Uitableview,UITableView具有确定哪些单元格当前可见的方法。我想知道的是一个细胞有多少是可见的 例如,当您向下拖动表格时,表格顶部的“新可见”单元格不仅会出现,而且会一次出现一条(像素)线,直到整个单元格可见为止。当拖动表视图时,如何判断在任何给定时刻该单元格中有多少是可见的 我的最终目标是,当用户在表格上拖动时,根据在任何给定时间显示的视图的多少来更改单元格中显示的视图 有什么建议吗?我还没有测试过,但我会尝试以下方法: UITableViewCell *cell; UIView *parent

UITableView具有确定哪些单元格当前可见的方法。我想知道的是一个细胞有多少是可见的

例如,当您向下拖动表格时,表格顶部的“新可见”单元格不仅会出现,而且会一次出现一条(像素)线,直到整个单元格可见为止。当拖动表视图时,如何判断在任何给定时刻该单元格中有多少是可见的

我的最终目标是,当用户在表格上拖动时,根据在任何给定时间显示的视图的多少来更改单元格中显示的视图


有什么建议吗?

我还没有测试过,但我会尝试以下方法:

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

然后比较特定的矩形。

参见Vadim Yelagin的答案:这里解释了如何将单元格矩形转换为父表视图坐标,并确定它是否完全可见。

我使用了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];
}

}

以下是Swift中实施的上述方法的一个变体:

    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
是单元格中可见的部分。还可以添加一个步骤来计算可见单元格的比率(介于0和1之间):

    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
    }
要根据可见性更改视图外观(如上问题所述),此代码应包含在表视图的超类委托方法中


但是,这只会在单元格滚动时影响单元格。已经可见的单元格不会受到影响。对于这些,方法中应该应用相同的代码。

您可以尝试以下方法:

-(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];
            }
        }
    }
}

谢谢你,伙计,我还没有想过使用CGRectIntersection,现在很明显!