2012-05-08 125 views

回答

-6

你可以尝试让UITableViewCell

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
// returns nil if cell is not visible or index path is out of range 

这里的完整代码:

UITableViewCell *cell = [cellForRowAtIndexPath:appDelegate.currentMainIndexPath]; 

if (appDelegate.currentMainIndexPath != nil && cell !=nil) 
{ 
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
    appDelegate.currentMainIndexPath = nil; 
} 
+16

为什么这是公认的答案?该代码甚至不是有效的Objective-C,即使您修复了编译错误,如果单元格不可见,仍然会使用返回nil的方法,这意味着您无法滚动到某个不是“ t已经在屏幕上,并且滚动的全部点是使不可见的东西。 –

2

如果你的意思,“有没有在指数n细胞”,那么你只需要你的数据源到n比较大小

if (appDelegate.currentMainIndexPath != nil [datasource count] > n) 
{ 
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
    appDelegate.currentMainIndexPath = nil; 
} 

其中数据源是例如NSArray。

0

您还可以使用的UITableView的numberOfRowsInSection方法。

1

这是你如何做到这一点:

indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section] 

在背景:

if (indexPath.row < [self.tableView numberOfRowsInSection:indexPath.section]) { 
    [self.tableView scrollToRowAtIndexPath:indexPath 
          atScrollPosition:UITableViewScrollPositionTop 
            animated:YES]; 
} 

插入到您的代码:

if (appDelegate.currentMainIndexPath != nil && 
    indexPath.row < [tblView numberOfRowsInSection:indexPath.section]) { 
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath 
        atScrollPosition:UITableViewScrollPositionTop 
          animated:NO]; 
    appDelegate.currentMainIndexPath = nil; 
} 
+2

如果段错误,'-numberOfRowsInSection:'将返回'NSNotFound',所以你应该检查它。或者更好的检查'indexPath.section <[self.tableView numberOfSections]',因为'NSNotFound'没有记录。 – kpower

26

您可以使用此。它传递indexpath的行和部分

-(BOOL) isRowPresentInTableView:(int)row withSection:(int)section 
{ 
    if(section < [self.tableView numberOfSections]) 
    { 
     if(row < [self.tableView numberOfRowsInSection:section]) 
     { 
      return YES; 
     } 
    } 
    return NO; 
} 
+1

使用NSUInteger或行> = 0 && section> = 0过滤一些错误的情况。 –

15

卡姆兰汗的回答迅速适应:

extension UITableView { 
    func hasRowAtIndexPath(indexPath: NSIndexPath) -> Bool { 
    return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRowsInSection(indexPath.section) 
    } 
} 

斯威夫特4:

extension UITableView { 
    func hasRow(at indexPath: IndexPath) -> Bool { 
     return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section) 
    } 
} 
+0

显然似乎是一个有效的答案 –

+0

这真的应该是Swift 3+的公认答案。感谢您的改编。 – Brian

3

还有一个更方便的方法来判断一个indexPath有效:

对于Swift 3.0:

open func rectForRow(at indexPath: IndexPath) -> CGRect 

对于Objective-C的

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath; 

您将获得CGRectZero如果indexPath是无效的。

func isIndexPathValid(indexPath: IndexPath) -> Bool { 
    return !tableView.rectForRow(at: indexPath).equalTo(CGRect.zero) 
} 
相关问题