How can I tell if a UITableView contains a specific NSIndexPath?

13,943

Solution 1

A Swift adaptation of Kamran Khan's answer:

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

Swift 4:

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

Solution 2

You can use this. Pass it indexpath's row and section

Objective C:

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

Swift 3:

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

    return false
}

Solution 3

There is a more convenient method to tell if a indexPath is valid:

For Swift 3.0:

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

For Objective-C

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

You will get CGRectZero if the indexPath is invalid.

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

Solution 4

If you mean, 'is there a cell at index n' then you just have to compare the size of you datasource to n

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

Where datasource is for instance an NSArray.

Solution 5

This is how you do it:

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

In context:

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

Inserted into your code:

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

Related videos on Youtube

Ethan Allen
Author by

Ethan Allen

Always learning.

Updated on June 04, 2022

Comments

  • Ethan Allen
    Ethan Allen almost 2 years

    Here is the code I'm using:

    if (appDelegate.currentMainIndexPath != nil /* && doesPathExistInTableView */)
    {
        [tblView scrollToRowAtIndexPath:appDelegate.currentMainIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        appDelegate.currentMainIndexPath = nil;
    }
    
  • Erik B
    Erik B about 10 years
    Why is this the accepted answer? The code is not even valid Objective-C and even if you fix the compile errors, you'd still be using a method that returns nil if the cell isn't visible, which means that you cannot scroll to something that isn't already on the screen and the whole point of scrolling is to make something that isn't visible visible.
  • kpower
    kpower about 10 years
    If section is wrong, -numberOfRowsInSection: will return NSNotFound, so you should check it also. Or better check indexPath.section < [self.tableView numberOfSections], because NSNotFound isn't documented.
  • Nabeel Ahmed
    Nabeel Ahmed almost 8 years
    Apparently Seems a valid answer
  • Shu Zhang
    Shu Zhang almost 8 years
    Use NSUInteger or row >= 0 && section >=0 to filter some false cases.
  • Brian
    Brian almost 7 years
    This really should be the accepted answer for Swift 3+. Thank you for the adaptation.
  • ScottyBlades
    ScottyBlades over 4 years
    The call to numberOfRows(inSection: indexPath.section) is causing a crash: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myapp tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fe052825a00'
  • ScottyBlades
    ScottyBlades over 4 years
    The call to numberOfRows(inSection: indexPath.section) is causing a crash: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myapp tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fe052825a00'