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

uitableview visible cells indexpath swift
uitableview check if cell is fully visible
check if uitableviewcell is visible swift
indexpathsforvisiblerows
uitableview visible cells swift
get indexpath of uitableviewcell swift
uitableview get indexpath of last row swift
reload particular cell in uitableview objective c

Here is the code I'm using:

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

You could try to get UITableViewCell by :

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

here's the complete code :

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

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

indexPathsForVisibleRows, If no rows are visible, the value is nil . See Also. Getting Cells and Section-Based Views. func cellForRow(at: IndexPath) -> UITableViewCell? Returns the Returns an index path representing the row and section of a given table-view cell . Starting with iOS 8, table view cells can be auto-sized, and no longer require the GetHeightForRow(UITableView, NSIndexPath) to be implemented to size cells. To do this, the cell constructor should set the ContentView constraints.

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
}

reloadRows(at:with:), Reloads the specified rows using a given animation effect. func reloadRows(at indexPaths: [IndexPath], with animation: UITableView. See UITableView. A Boolean value indicating whether the table view's appearance contains changes � Create a view controller, and drag a UITableView into the view, as shown below. Step 2: Create a table view outlet. Control-drag from the view to the implementation file to create an outlet to the UITableView. Step 3: Set the datasource and delegate. Declare that the class implements the table view datasource and delegate protocols.

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

UITableViewSource Class (UIKit), UITableViewSource merges both the UITableViewDataSource and the UITableViewDelegate into a single class. Whether the row at the specified indexPath may receive focus. Indicates that the cell at the specified indexPath has finished displaying. Invoked to determine if this object implements the specified protocol. Tag: ios,uitableview,swift. I have a UITableView with more cells than fit on screen. When I get a notification from my data model I want to jump to a specific row and show a very basic animation. My code is:

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

Everything you need to know about iOS UITableView, Everything you need to know about iOS UITableView if indexPath.row > 0 && indexPath.row < info.count + 1 { Instead of using extra parameter to pass selected row index, like in this You can even define multiple DataSource objects, when your table has very logically separated sections, like this: Adding Index List to UITableView. So how can you add an index list to the table view? Again it’s easier than you thought and can be achieved by a few lines of code. Just add the sectionIndexTitlesForTableView: method and return an array of the section indexes. Here we use the section titles as the indexes.

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.

Dealing with Complex Table Views in iOS and Keeping Your Sanity , These events include new photos and status messages, grouped by day. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) What's more, cell-specific logic is now in a single place, instead of being So, you end up having to check whether you can cast into a type, and then� ios,swift,uitableview When I expand a UITableViewCell on touch, I know I have to update the UITableView. Right now I'm doing: tableView.beginUpdates() tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic) tableView.endUpdates() Do I need to use both updates methods as well as the reload method?

Simplifying IndexPath Logic - Swift Talk, 01:06 Looking at our table, we see that each section has a number of static cells 01:33 The table view controller will have a sections array, and when we func tableView(_ tableView: UITableView, cellForRowAt indexPath:� NSIndexPath() Default constructor that initializes a new instance of this class with no parameters. NSIndexPath(IntPtr) A constructor used when creating managed representations of unmanaged objects; Called by the runtime. NSIndexPath(NSCoder) A constructor that initializes the object from the data stored in the unarchiver object.

Uitableview scroll to last row swift 4, I would like it so that when the user clicks on a particular person (thus *) indexPath However, this will only tell you if a tap occurs on an existing row. I am attempting to make the last row in a UITableView visible, after it has been added. The UITableViewSource's NumberOfSections(UITableView) and RowsInSection(UITableView, nint) methods allow the UITableView to request only the data necessary for the cells on the screen. A specific cell is identified by an NSIndexPath , whose Section and Row properties will specify a unique cell.

UITableViewController - iOS & Swift Tutorial, For this tutorial you need basic programming skills and know how to use Xcode. You should know A Basic Cell has one Label by default: Set some country Make sure that iOS is selected to create a source file for iOS: textLabel?.text = " Section \(indexPath.section) Row \(indexPath.row)" return cell }. ios,xcode,swift,uitableview,tableviewcell Because you are using reusable cells when you try to select a cell that is not in the screen anymore the app will crash as the cell is no long exist in memory, try this: if let lastCell = self.diceFaceTable.cellForRowAtIndexPath(lastIndexPath) as!

Comments
  • 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.
  • Use NSUInteger or row >= 0 && section >=0 to filter some false cases.
  • Apparently Seems a valid answer
  • This really should be the accepted answer for Swift 3+. Thank you for the adaptation.
  • 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'
  • 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'
  • 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.