Jim*_*m B 54 iphone uitableview
有没有办法知道tableview单元格当前是否可见?我有一个tableview,其第一个单元格(0)是一个uisearchbar.如果搜索未激活,则通过偏移隐藏单元格0.当表只有几行时,第0行是可见的.如何确定第0行是可见还是第一行?
Dev*_*red 100
UITableView有所谓的一个实例方法indexPathsForVisibleRows,将返回NSArray的NSIndexPath在当前可见的表对象的每一行.您可以使用您需要的任何频率检查此方法并检查正确的行.例如,如果tableView是对表的引用,则以下方法将告诉您行0是否在屏幕上:
-(BOOL)isRowZeroVisible {
NSArray *indexes = [tableView indexPathsForVisibleRows];
for (NSIndexPath *index in indexes) {
if (index.row == 0) {
return YES;
}
}
return NO;
}
Run Code Online (Sandbox Code Playgroud)
因为该UITableView方法返回NSIndexPath,所以您可以轻松地扩展它以查找节或行/节组合.
这比visibleCells返回表单元格对象数组的方法更有用.表格单元格对象会被回收,因此在大型表格中,它们最终不会与数据源产生简单的关联.
Ana*_*hra 41
要检查tableview单元格是否可见,请使用此代码行
if(![tableView.indexPathsForVisibleRows containsObject:newIndexPath])
{
// your code
}
Run Code Online (Sandbox Code Playgroud)
这里newIndexPath是检查单元格的IndexPath .....
Swift 3.0
if !(tableView.indexPathsForVisibleRows?.contains(newIndexPath)) {
// Your code here
}
Run Code Online (Sandbox Code Playgroud)
Emi*_*mil 14
我在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 }
} }
Run Code Online (Sandbox Code Playgroud)
小智 5
Swift版本:
if let indices = tableView.indexPathsForVisibleRows {
for index in indices {
if index.row == 0 {
return true
}
}
}
return false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50390 次 |
| 最近记录: |