Can you downcast a NSIndexPath to NSInterger - ios

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.

Related

Need to create an NSIndexPath for iCarousel to pass info to additional view controllers

I'm implementing an iCarousel that should pass information based on selection made to two additional view controllers.
To pass the information I understand that I need to create an NSIndexPath in the prepareForSegue method in the view controller that contains the iCarousel. The problem I'm having is creating an NSIndexPath since the carousel does not have rows.
In a tableview situation I could use the following:
NSIndexPath *path = [self.tableview indexPathForSelectedRow];
Can I simply change "tableview" to "carousel" and use something in place of indexPathForSelectedRow or is it more complicated than that?
As for creating the indexPath object:
You can create an indexPath with the
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:itemID inSection:0];
Giving a 0 value to the section index would not matter since, as you point out, there is no section in the carrousel
Finding the index for the itemID, if you use the nicklockwood's iCarousel :
int itemId = [self.myCarroussel currentItemIndex]

How to select row in uitableview programmatically based on NSString

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

When a row is selected in the table, do something on the other table?

I have three tables in a view, I need to implement this kind of structure;
I need to control if the row is selected in the first table, then adding rows of the third table to other view. when i press the button, the data of the selected and the rows in the third table added to view will be sent.
Firstly, i am unable to recognize whether the row is selected or not,
Secondly, when i try just to check it works, my data dictionary sent is empty, that should not.
My code is as below;
-(void) shareComment : (id)sender
{
int ndx = [[tableViewPharmName indexPathForSelectedRow] row];
ProdForLiterature *temp = [pharmsTable objectAtIndex:ndx];
[sendData setObject:temp.productID forKey:#"ProductName"];
[sendData setObject:temp.productName forKey:#"ProductId"];
NSMutableArray *customerId = [[NSMutableArray alloc]init];
for(DoctorListCell *cell in tableViewDoctorName.visibleCells)
{
if([cell.plusButton isHidden])
{
if(cell.customerId!=nil)
{
[customerId addObject:cell.customerId];
}
}
}
[sendData setObject:customerId forKey:#"CustomerId"];
}
For the first question. To know if a row is selected you can use this method.
NSIndexPath *path = [tableView indexPathForSelectedRow];
int rowSelected = path.row
For the second question, try first logging the temp object to see if it is empty.
NSLog(#"temp:%#",temp);
Also, check after this line.
[sendData setObject:customerId forKey:#"CustomerId"];
With another log to see if sendData contains information or it is initialised.
NSLog(#"sendData:%#",sendData);
Let us know what results you get.

UICollectionView, scrolling to an Item without knowing its NSIndexPath

I have a UIcollectionview that shows an array of objects called Items.
At one point in the lifecycle of my app I do need to move the collectionview ( or scroll it ) to a specific Item that I receive Via NSnotification. I do not know the NSIndexPath of that Item but its definitely available in the collectionview.
I tried
NSIndexPath *myIndexPath = [NSIndexPath indexPathForItem:myItem inSection:1];
[self.collectionView scrollToItemAtIndexPath:myIndexPath
atScrollPosition:UICollectionViewScrollPositionCenteredVertically
animated:NO];
But this gives back an arbitrary number and not relevant.
How can I do that?
I dont know details of code so i am generalising my answer.
You can have a unique property for myItem say ID.
If you are maintaining an array of items say : myItemArray, and populating collection view in same order then following can work:
NSArray* myItemArray; //array used to populate collectionView
NSIndexPath *myIndexPath; //this is what we need to know
int index = 0;
//myItem is object of item type & notifiedItem is notified item
for (Item* myItem in myItemArray) {
if (myItem.ID == notifiedItem.ID) {
myIndexPath = [NSIndexPath indexPathForItem:index inSection:1];//you got the index path right
break;
}
index++;
}
//use the index path

how to populate a static UITableView

I tried several ways but it seems I'm missing something..
Here is the code I use:
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
for (int idx=0; idx<16; idx++) {
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:idx];
[self.tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text = [param objectAtIndex:idx];
}
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
I call this on viewDidLoad. My tableview has 16 static cells, and I'm using standard "Subtitle" type cell, so no customization needed on that front. textLabel.text is filled at design time. Also my tableview is in a tableViewController. I also tried with standard population of tableview but it seems static cells don't agree with that way.
What am I doing wrong?
#jrturton I did some changes to see what's going on:
I added these three lines under for line to see if there's anything there:
UITableView *tv = self.tableView;
UITableViewCell *cell = [tv cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
NSString *label = [[NSString alloc] initWithString:cell.textLabel.text];
First of all tv is assigned correctly, I can see that. But other than that cell and label comes empty. Even tv has no row information it seems..
Could it be because tableview has datasource and delegate assigned to the tableviewcontroller.. I think I heard it shouldn't be like that or something...
#jrturton Here it is:
Hope this helps.
Some logging would quicky show you that your index path is not valid and therefore no cell is being returned.
To create an NSIndexPath for use in a UITableView, use this method:
[NSIndexPath indexPathForRow:idx inSection:0];
The index path has to contain a row and section for the table to understand it.
You also need to call this from viewWillAppear: rather than viewDidLoad, the static table is not populated that early on. And you must call [super viewWillAppear:animated] first!

Resources