【问题标题】:How can I tell if a UITableView contains a specific NSIndexPath?如何判断 UITableView 是否包含特定的 NSIndexPath?
【发布时间】:2012-05-17 09:23:50
【问题描述】:

这是我正在使用的代码:

if (appDelegate.currentMainIndexPath != nil /* && doesPathExistInTableView */)
{
    [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    appDelegate.currentMainIndexPath = nil;
}

【问题讨论】:

    标签: iphone objective-c ios xcode cocoa-touch


    【解决方案1】:

    我迭代了Kamran's answer

    + (BOOL)isIndexPath:(NSIndexPath *)indexPath inTableView:(UITableView *)tableView {
        if (!indexPath) {
            return NO;
        }
        if (indexPath.section < [tableView numberOfSections]) {
            if (indexPath.row < [tableView numberOfRowsInSection:indexPath.section]) {
                return YES;
            }
        }
        return NO;
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用它。传递 indexpath 的行和节

      目标 C:

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

      斯威夫特 3:

      func isRowPresentInTableView(indexPath: IndexPath) -> Bool{
          if indexPath.section < tableView.numberOfSections{
              if indexPath.row < tableView.numberOfRows(inSection: indexPath.section){
                  return true
              }
          }
      
          return false
      }
      

      【讨论】:

      • 使用 NSUInteger 或 row >= 0 && section >=0 过滤一些错误的情况。
      【解决方案3】:

      对 Kamran Khan 的回答的 Swift 改编:

      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)
          }
      }
      

      【讨论】:

      • 显然似乎是一个有效的答案
      • 这确实应该是 Swift 3+ 的公认答案。感谢您的改编。
      • numberOfRows(inSection: indexPath.section) 的调用导致崩溃:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myapp tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fe052825a00'
      【解决方案4】:

      有一种更方便的方法来判断 indexPath 是否有效:

      对于 Swift 3.0:

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

      对于 Objective-C

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

      如果 indexPath 无效,你会得到 CGRectZero。

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

      【讨论】:

      • numberOfRows(inSection: indexPath.section) 的调用导致崩溃:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myapp tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fe052825a00'
      【解决方案5】:

      这就是你的做法:

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

      【讨论】:

      • 如果部分错误,-numberOfRowsInSection: 将返回NSNotFound,所以你也应该检查它。或者更好地检查indexPath.section &lt; [self.tableView numberOfSections],因为NSNotFound 没有记录。
      【解决方案6】:

      你也可以使用 UITableView 的numberOfRowsInSection 方法。

      【讨论】:

        【解决方案7】:

        您可以尝试通过以下方式获取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;
        }
        

        【讨论】:

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

        如果您的意思是“索引 n 处是否有一个单元格”,那么您只需将数据源的大小与 n 进行比较

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

        其中数据源例如是一个 NSArray。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-03-06
          • 1970-01-01
          • 2019-08-17
          • 2011-02-05
          • 2016-05-10
          • 2021-02-07
          • 2014-11-02
          • 1970-01-01
          相关资源
          最近更新 更多