52

I need to convert a nsindexpath var into NsInteger or simply an int. e.g:

 int rowIndex = [mGoogleBaseTable selectedRow];
 //mGoogleBaseTable is a NSTable type in Macdevelopement..

&&

int rowIndex = [mGoogleBaseTable indexPathForSelectedRow];
//mGoogleBaseTable is a UITableView type in Iphone (iOS)Development.

selectedRow returns NSInteger simply but indexPathForSelectedRow returns a NSIndexPath..

Please help me how can i achieve this.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Tarun
  • 766
  • 1
  • 5
  • 12

6 Answers6

132

If

NSIndexPath *p = ... // some indexpath

then

NSInteger row = p.row;
NSInteger section = p.section;

That's all!

mvds
  • 45,755
  • 8
  • 102
  • 111
14

Maybe, this is can help you

NSInteger variable = indexPath.row;
PS.OHM
  • 473
  • 1
  • 4
  • 9
  • also can used like yourTextField.tag = indexPath.row to track and create conditions inside your your custom cells, say if you want to update data inside a mutable array.. – whyoz Oct 03 '13 at 00:53
8

In Swift:

    let section: Int = indexPath.section
    let row: Int = indexPath.row
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
7

I prefer doing it this way

NSNumber *selRow = [[NSNumber alloc] initWithInteger:indexPath.row];
verma
  • 753
  • 8
  • 8
1

Simply convert into int by,

NSIndexPath *path = ... // some indexpath

int row = (int)path.row;

Pavitha
  • 181
  • 2
  • 10
-1

In swift :

  1. Associate your collection view to your var :

    @IBOutlet weak var collectionView: UICollectionView!

  2. In prepareForSegue func, you can call :

    var indexPath = collectionView.indexPathsForSelectedItems()

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
Wiza
  • 1
  • 1