Get Selected index of UITableView

IosObjective CIphoneUitableviewNsindexpath

Ios Problem Overview


I want to have selected index for UITableView. I have written following code:

NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
[tableView scrollToRowAtIndexPath:index 
                 atScrollPosition:UITableViewScrollPositionTop 
                         animated:YES];

This always work for 1st item. I want to have selected index in indexPathForRow.

Please help. Thanks.

Ios Solutions


Solution 1 - Ios

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

Solution 2 - Ios

Get current selected row. May be helpful if you have only one section.

- (NSUInteger)currentSellectedRowIndex
{
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    if (selectedIndexPath) {
        return selectedIndexPath.row;
    }
    else {
        return NSNotFound;
    }
}

Solution 3 - Ios

Use this code

CGPoint location =[sender locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell  = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:swipedCell];

Solution 4 - Ios

In didSelectRowAtIndexPath, set an interface declared NSIndexPath as the indexpath returned. Then you can scroll to that variable. If you need help, comment and I can give some sample code.

Solution 5 - Ios

Swift 4.2

let selectedIndexPath = tableView.indexPathForSelectedRow

which is an optional return value.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniOSDevView Question on Stackoverflow
Solution 1 - IosChris GummerView Answer on Stackoverflow
Solution 2 - IosNuzhdin VladimirView Answer on Stackoverflow
Solution 3 - IosVineesh TPView Answer on Stackoverflow
Solution 4 - IosKolin KrewinkelView Answer on Stackoverflow
Solution 5 - IosdavidrynnView Answer on Stackoverflow