时间:2010-07-24 19:58:11

标签: iphone uitableview

9 个答案:

答案 0 :(得分:98)

答案 1 :(得分:40)

要查看tableview单元格是否可见,请使用此行代码

    if(![tableView.indexPathsForVisibleRows containsObject:newIndexPath])
    {
       // your code 
    }

这里newIndexPath是检查单元格的索引路径.....

Swift 3.0

if !(tableView.indexPathsForVisibleRows?.contains(newIndexPath)) {
  // Your code here

}

答案 2 :(得分:12)

我在Swift 3.0中使用它

extension UITableView {

    /// Check if cell at the specific section and row is visible
    /// - Parameters:
    /// - section: an Int reprenseting a UITableView section
    /// - row: and Int representing a UITableView row
    /// - Returns: True if cell at section and row is visible, False otherwise
    func isCellVisible(section:Int, row: Int) -> Bool {
        guard let indexes = self.indexPathsForVisibleRows else {
            return false
        }
        return indexes.contains {$0.section == section && $0.row == row }
    }  }

答案 3 :(得分:5)

Swift版本:

if let indices = tableView.indexPathsForVisibleRows {
    for index in indices {
        if index.row == 0 {
            return true
        }
    }
}
return false

答案 4 :(得分:4)

另一个解决方案(也可用于检查行是否完全可见)将检查row的帧是否在tableview的可见矩形内。

在以下代码中,ip代表NSIndexPath

CGRect cellFrame = [tableView rectForRowAtIndexPath:ip];
if (cellFrame.origin.y<tableView.contentOffset.y) { // the row is above visible rect
  [tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
else if(cellFrame.origin.y+cellFrame.size.height>tableView.contentOffset.y+tableView.frame.size.height-tableView.contentInset.top-tableView.contentInset.bottom){ // the row is below visible rect
  [tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}

同样使用cellForRowAtIndexPath:应该有效,因为如果行不可见,它会返回nil个对象:

if([tableView cellForRowAtIndexPath:ip]==nil){
  // row is not visible
}

答案 5 :(得分:1)

IOS 4:

NSArray *cellsVisible = [tableView indexPathsForVisibleRows];
NSUInteger idx = NSNotFound;
idx = [cellsVisible indexOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
    return ([(NSIndexPath *)obj compare:indexPath] == NSOrderedSame);
}];

if (idx == NSNotFound)
{

答案 6 :(得分:1)

快捷键:

改进的@Emil答案

extension UITableView {
    
    func isCellVisible(indexPath: IndexPath) -> Bool {
        guard let indexes = self.indexPathsForVisibleRows else {
            return false
        }
        return indexes.contains(indexPath)
    }
}

答案 7 :(得分:0)

请注意,“有些可见”是“可见的”。此外,在viewWillAppear中,当布局正在进行时,您可能会从indexPathsForVisibleRows获得误报,如果您正在查看最后一行,即使调用layoutIfNeeded()也无法帮助您处理表格。您需要查看viewDidAppear之后/之后的内容。

如果最后一行完全可见,则此快速3.1代码将禁用滚动。在viewDidAppear中/之后调用它。

    let numRows = tableView.numberOfRows(inSection: 0) // this can't be in viewWillAppear because the table's frame isn't set to proper height even after layoutIfNeeded()
    let lastRowRect = tableView.rectForRow(at: IndexPath.init(row: numRows-1, section: 0))
    if lastRowRect.origin.y + lastRowRect.size.height > tableView.frame.size.height {
        tableView.isScrollEnabled = true
    } else {
        tableView.isScrollEnabled = false
    }

答案 8 :(得分:0)

最好按帧检查

let rectOfCellInSuperview = yourTblView.convert(yourCell.frame, to: yourTblView.superview!) //make sure your tableview has Superview
if yourTblView.frame.contains(rectOfCellInSuperview) == false {
       //Your cell is not visible
}
相关问题