This question already has answers here:
Convert NSInteger to NSIndexpath
(4 answers)
Closed 7 years ago.
I am making a basic quiz application.The choices are in the form of a table view. If the user moves to a previous question, I want to show him his previously selected choice. I have stored the choice in NSUserDefaults. How do I convert this int value to NSIndexpath and use it in my tableview?
See the NSINdexPath UIKit Additions
In your case you can use (Swift):
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
Swift 3.0:
let indexPath = IndexPath(row: 0, section: 0)
Duplicate of This post
I copied the recieved answer here
For an int index:
NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
Creates Index of the item in node 0 to point to as per the reference.
To use the indexPath in a UITableView, the more appropriate method is
NSIndexPath *path = [NSIndexPath indexPathForRow:yourInt inSection:section];
You can get indexpath using below code
NSIndexPath *index = [NSIndexPath indexPathForRow:YOURINTSTORED inSection:0];
Related
I have a AVPlayer class that I'm using in a detail view that takes a indexPath of type Int.
I am getting the indexPath from the table view tableViewDidSelectAtIndexPath before sending it to the detail VC to use.
I tried to just simply downcast the NSIndexPath as! NSInteger before sending it to detail VC to use but it crashes without a crash message. So Im a bit stuck as to how to go about this.
Is it possible to downcast this? How can I get the number out of the NSIndexPath into an Int so I can use it in my class?
Objective C
NSInteger currentRow = indexPath.row;
Swift
let currentRow = self.tableView.indexPathForSelectedRow().row
NSIndexPath is always* a combination of section and either row (for UITableView) or item (for UICollectionView).
you need to dissolve the section by accessing the single index:
id object = [[self dataSource] objectAtIndex:[indexPath row]];
* EDIT
Respective the comments: you are right. Multiple indexes are possible with NSIndexPath.
If you know you have multiple indexes (what I wouldn't expect in UITableViewDelegate) you can iterate the indexes:
for (int i = 0; i < [indexPath length]; i++) {
NSUInteger index = [indexPath indexAtPosition:i];
// use index to access whatever you want
}
EDIT
PLEASE read documentation and headers before you ask the next time. Little hint: cmd-click NSIndexPath will take you to its header file with all possible informations.
I am trying to create accordion tableview. here I can get easily parent tableview selected cell index path values but I cant get subtable (Expandable child tableview cell) row index path. Please give me any solution for my issue and find below code
My Code :
// #optional
- (void)didSelectRowAtChildIndex:(NSInteger)childIndex underParentIndex:(NSInteger)parentIndex {
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[self rowForParentIndex:parentIndex] inSection:0];
UITableViewCell *selectedCell = [self cellForRowAtIndexPath:indexPath];
if ([selectedCell isKindOfClass:[ParentTableViewCell class]]) {
//ParentTableViewCell * pCell = (ParentTableViewCell *)selectedCell;
// Insert code here to detect and handle child cell selection
// ...
}
}
I am checking by open source code : https://github.com/ajkoshy7/SubTable
Get indexpath of selected Child from childIndex:
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:[self rowForParentIndex:parentIndex] inSection:0];
NSIndexPath * indexPathChild = [NSIndexPath indexPathForRow:[self rowForParentIndex:childIndex] inSection:indexPath.row];
I am a new IOS Programmer, and i am having a issue.
I have a UITableView with 2 sections, one then is static, and another one is dynamical.
In specific actions i need to add new rows for the second section in runtime..
I know how to manage a UITableView , but not a specific section
Could you help me please?
Best Regards you all
You can use insertRowsAtIndexPaths: method of UITableView
//Update data source with the object that you need to add
[tableDataSource addObject:newObject];
NSInteger row = //specify a row where you need to add new row
NSInteger section = //specify the section where the new row to be added,
//section = 1 here since you need to add row at second section
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];
You can use the same method insertRowAtIndexPath like
// Add the items into your datasource object first. Other wise you will end up with error
// Manage the number of items in section. Then do
NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:1];
NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:1 inSection:1];
[self.tableView insertRowsAtIndexPaths:#[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];
This will insert two rows to the section1. Remember before doing this you have manage your datasource object.
Swift 3 version
// Adding new item to your data source
dataSource.append(item)
// Appending new item to table view
yourTableView.beginUpdates()
// Creating indexpath for the new item
let indexPath = IndexPath(row: dataSource.count - 1, section: yourSection)
// Inserting new row, automatic will let iOS to use appropriate animation
yourTableView.insertRows(at: [indexPath], with: .automatic)
yourTableView.endUpdates()
you can do this by using scrolltoinfinite method but before that u have to import 3rd party svpulltorefresh
[self.tableViewMixedFeed addInfiniteScrollingWithActionHandler:^{
CGFloat height = self.tableViewMixedFeed.frame.size.height;
CGFloat contentYoffset = self.tableViewMixedFeed.contentOffset.y;
CGFloat distanceFromBottom = self.tableViewMixedFeed.contentSize.height - contentYoffset;
if(distanceFromBottom<contentYoffSet)
{
[weak insertRowAtBottom];
}
}];
-(void)insertRowAtBottom
{
[self.tableViewMixedFeed beginUpdates];
[self.tableViewMixedFeed insertRowsAtIndexPaths:indexPaths1 withRowAnimation:UITableViewRowAnimationTop];
[self.tableViewMixedFeed endUpdates];
[self.tableViewMixedFeed.infiniteScrollingView stopAnimating];
}
here indexpaths1 is the array of indexpaths of next cells which u want to insert in a table .
try using loop to get the next set of arrays and store them into indexpaths1.
[self.tableView insertRowsAtIndexPaths:#[indexPath1,indexPath2] withRowAnimation:UITableViewRowAnimationTop];
USE THIS METHOD..
I have a uitableview that hosts a number of cells, in another part of the app the user will make an NSString selection that will match on of the cells in the uitableview, how can I select hat cell programmatically?
I tried using the method
selectRowAtIndexPath:animated
but how can I get the indexpath? is there a better way?
I think you are loading data from an array. Then just search inside that NSArray and take index of the matched object. Now you can create NSIndexPath programatically:
[NSIndexPath indexPathForRow:index inSection:section];
Just use this NSIndexPath to select the row
try this .... first try to get the cell number based on string
NSString * pUserSelectedString = #"abcd";
int index;
if([array containsObject:pUserSelectedString])
{
index = [array indexOfObject:pUserSelectedString];
}
The way you needs to get the indexpath from source array is
int selectionIndex;
if([sourceArray containsObject:userSelectionString])
{
selectionIndex = [sourceArray indexOfObject:userSelectionString];
}
You have to check whether selection object is there or not before get the indexpath from sourceArray.then you have to use this indexpath for selectRowAtIndexpath:animated method
Basically I am storing an index of an array in a NSInteger. I now need it as an NSIndexpath, I'm struggling to see and find a way to convert my NSInteger to NSIndexpath so I can reuse it.
For an int index:
NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
Creates Index of the item in node 0 to point to as per the reference.
To use the indexPath in a UITableView, the more appropriate method is
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
If you need a NSIndexPath for a UITableView, you can use indexPathForRow:inSection: (reference). Index paths passed to table view must contain exactly two indices specifying the section and row. An index path created with indexPathWithIndex: only contains one index and won't work with a table view.
Use below methods of NSIndexPath class.
+ (id)indexPathWithIndex:(NSUInteger)index;
+ (id)indexPathWithIndexes:(NSUInteger *)indexes length:(NSUInteger)length;
- (id)initWithIndex:(NSUInteger)index
Use as below and also Don't forget to release myIndexPath object after using.
NSIndexPath *myIndexPath = [[NSIndexPath alloc] initWithIndex:[myIntObj intValue]];
Swift 3:
let indexPath = IndexPath(row: 0, section: 0)