Deepak R
Deepak R

Reputation: 283

How to create an NSIndexPath from an int?

Error message:

cast of 'int' to "NSIndexPath *' is disallowed with ARC

Code:

NSInteger CurrentIndex;

[self tableView:(UITableView *)horizontalTableView1 didSelectRowAtIndexPath:(NSIndexPath *)int_CurrentIndex];

How do I fix this error?

Upvotes: 20

Views: 30244

Answers (2)

TheTiger
TheTiger

Reputation: 13354

You can't cast an int variable to an NSIndexPath directly. But you can make an NSIndexPath from an int variable.

You need a section index and row index to make an NSIndexPath. If your UITableView has only one section then:

Objective-C

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];

Swift

let indexPath: NSIndexPath = NSIndexPath(forRow: rowIndex, inSection: 0)

Swift 4

let indexPath = IndexPath(row: rowIndex, section: 0)

Upvotes: 83

Chandramani
Chandramani

Reputation: 891

Swift 3.0

let indexPath : IndexPath = IndexPath(item: currentIndex, section: 0)

Upvotes: 4

Related Questions