如何判断UITableView是否包含特定的NSIndexPath?

Eth*_*len 15 iphone xcode cocoa-touch objective-c ios

这是我正在使用的代码:

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

Kam*_*han 33

你可以用它.传递indexpath的行和部分

目标C:

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

斯威夫特3:

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

    return false
}
Run Code Online (Sandbox Code Playgroud)


Pet*_*ter 25

快速改编Kamran Khan的答案:

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

斯威夫特4:

extension UITableView {
    func hasRow(at indexPath: IndexPath) -> Bool {
        return indexPath.section < self.numberOfSections && indexPath.row < self.numberOfRows(inSection: indexPath.section)
    }
}
Run Code Online (Sandbox Code Playgroud)