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;
}
Related videos on Youtube
Comments
-
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 about 10 yearsWhy 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 about 10 yearsIf section is wrong,
-numberOfRowsInSection:
will returnNSNotFound
, so you should check it also. Or better checkindexPath.section < [self.tableView numberOfSections]
, becauseNSNotFound
isn't documented. -
Nabeel Ahmed almost 8 yearsApparently Seems a valid answer
-
Shu Zhang almost 8 yearsUse NSUInteger or row >= 0 && section >=0 to filter some false cases.
-
Brian almost 7 yearsThis really should be the accepted answer for Swift 3+. Thank you for the adaptation.
-
ScottyBlades over 4 yearsThe 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 over 4 yearsThe 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'