如何判断UITableViewCell的可见程度

时间:2012-02-22 04:01:40

标签: ios uitableview

UITableView具有确定当前可见的单元格的方法。我想要找出的是一个细胞有多少是可见的。

例如,当您向下拖动表格时,表格顶部的“新显示”单元格不会出现,但一次只显示一条(像素)行,直到整个单元格可见。当拖动表格视图时,如何判断在任何给定时刻该单元格的可见程度。

我的最终目标是,当用户拖动桌面时,根据在任何给定时间可见的多少来更改单元格内的显示视图。

有什么建议吗?

5 个答案:

答案 0 :(得分:5)

您可以尝试这样的事情:

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

答案 1 :(得分:4)

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

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

然后比较特定的矩形。

答案 2 :(得分:3)

以下是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
    }

根据可见性更改视图外观 - 如上所述 - 此代码应包含在表格视图的UIScrollView超类委托UIScrollViewDelegate方法scrollViewDidScroll中。

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

答案 3 :(得分:2)

请参阅Vadim Yelagin的答案:https://stackoverflow.com/a/9843146/758298,其中解释了如何将单元格矩形转换为父表格视图坐标,并确定它是否完全可见。

答案 4 :(得分: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];
}

}

相关问题