Which number is the first object in a NSArray? I originally thought it was 0 but I may be wrong.
Also which number is the first cell in a UITableView? I thought the first indexpath.row was 1 but I also may be wrong?
Are there any links anyone can point me to, where Apple explains this?
Thanks!
-(BOOL) textFieldShouldReturn:(UITextField *)thetextField {
CustomCell *cell = (CustomCell*)[thetextField superview];
NSIndexPath *indexPath = [thetableView indexPathForCell:cell];
NSLog(#"%i", indexPath.row);
Index 0 represents the first element in NSArray.
As for UITableView, you have both a row and section number in an index path. So the first element of the first section is when indexPath.row = 0 and indexPath.section = 0.
In any array, the first object is at index 0. In case of a tableView, it is basically populated with an array, and the first object, which is indexPath.row == 0 denotes first row.
So the answer to both = 0 !
The first object should be 0 as objc is kind of C.
As for the first object of UITableView,it depends on your datasource. If your application has only a section, the datasource usually is a NSArray. Then the first cell corresponding to the datasource has the 0 index (we usually get the index through
-[NSIndexPath row].
If you have several sections, the the datasource usually is a NSArray of NSArray. Then the first cell corresponding to the datasource is the first object in the first NSArray in the datasource.
You should read the UITableView Programming Guide.
You are assuming that you get a cell with this:
CustomCell *cell = (CustomCell*)[thetextField superview];
Is that assumption correct?
Related
I have a method which is designed to clear all UITableViewCellAccessories and I get the following error when it is called
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.'
Here is the method
-(void)clearTableViewAccessories{
NSIndexPath *indexPath = [NSIndexPath new];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark){
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
The way you are initialising the indexPath object, creates an index path of length 0 and no path, which is formed by a section and a row.
You should create the indexPath with the designated initialiser
indexPathForRow:inSection:
Anyway I don't think your implementation is the best way to solve your problem. With your current setup you need to create an indexPath for every cell you want to create iterating through all of them, and you will get nil for any non-visible cell.
The best thing would be to start looking at UITableView dataSource methods, first of all tableView:cellForRowAtIndexPath: and inside here make the decision whether to clean the cell or not.
In your cellForRowAtIndexPath check if the cell should have no accessory. For example, you could modify your datasource or just use an ivar BOOL noAccessory. Set the accessory accordingly. Then simply call
[self.tableView reloadData];
Check your number or rows and number of sections , I had a similar issue which I solved by correctly populating number of rows in each section( if you have rows in section ). If you update the question with your data source eg array for section, and Rows , will be easier to answer
I am trying to trigger some code based on if the cell I selected becomes the first or last cell on the users screen. I'm not trying to capture the index value of the data array. Just the index value of the cell that is visible on the screen. I'm sure this to create an array of visible cells.
NSArray *indexPathsForVisibleRows = [myTableView
indexPathsForVisibleRows];
But I keep hitting a dead end trying to then capture an index value based on that array.
I tried to use a CGPoint and convert that, but I keep getting an error. Any insight would be most helpful!
As per the documentation for the return value of that method:
An array of NSIndexPath objects each representing a row
index and section index that together identify a visible row in the
table view. Returns nil if no rows are visible.
The array returned from that method contains NSIndexPaths.
NSArray *indexPathsForVisibleRows = [myTableView indexPathsForVisibleRows];
for(NSIndexPath *eachIndexPath in indexPathsForVisibleRows)
{
NSInteger row = eachIndexPath.row;
NSInteger section = eachIndexPath.section;
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:eachIndexPath];
if(cell.isSelected)
{
// this is our selected cell
}
}
I am trying to get the indexpath.section and indexpath.row for my prepareForSeague. I can pass the indexpath.row no problem however I am struggling to integrate it with sections. It passes the correct data across but not when outside the first section as I do not know how to set the sections.
So basically I need it to know which section it is in too.
I looked at objectsAtIndexes?
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
dynamicDetailTableViewController *destViewController = segue.destinationViewController;
NSLog(#" indexPath row %d",indexPath.row);
NSLog(#" indexPath section %d",indexPath.section);
// Assume self.view is the table view
destViewController.titleString = [self.namesArray objectAtIndex: indexPath.row];
//Health
destViewController.healthArray = [self.healthArray objectAtIndex:indexPath.row];
EDIT
To make it clearer, it does work however I have different sections so after the first section it doesn't pass the right value. It passes only the first Sections values correct as it is only calculating the indexpath based on the row and not the section. I need it to calculate the section too, but not sure how.
I guess your data structure is wrong, if you have multiple sections and rows. Your data structure should look something like below
Section 1
----> Row 1
----> Row 2
----> Row 3
Section 2
----> Row 1
----> Row 2
Section 3
----> Row 1
----> Row 2
----> Row 3
----> Row 4
And to fetch the values your code should be something like
[[self.array objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
The above code says give me particular row for the particular section.
In your case it doesn't seems to be the case, you have got all the data in a single array, where as it should be array of array.
You are getting wrong values because for each indexPath.section your indexPath.row starts with 0, so for 1st section it works fine, but not for other sections..
Hope it helps.
I have got the NSIndexPath from a uitableview before but I'm not sure why this doesn't work for collectionview? Any help would be greatly appreciated!
Here's the code I need to fix:
NSIndexPath *indexPath = [self.collectionView indexPathForSelectedRow];
sqlColumns *author = [self.favoriteExercises objectAtIndex:indexPath.row];
Just use indexPathsForSelectedItems in place of indexPathsForSelectedRow.
But this instruction will return more than one indexPath if you select more than one item. You should restrict the selection to one item by setting the allowsMultipleSelection of the UICollectionView to NO.
That said, Xcode has a brilliant autocompletion feature, you could have started to type "index" and it would have shown indexPathsForSelectedItems directly, you can also refer to the excellent documentation provided with Xcode. I Hope it will help you!
UPDATE 1 - How to grab the indexPath from the array
the indexPathsForSelectedItems method will return an array of NSIndexPath objects, each of which corresponds to a single selected item. If there are no selected items, this method returns an empty array.
Now to access this object, you have to ask yourself, how do I access an array? You do some research and then you will come to this conclusion:
NSArray *arrayOfIndexPaths = [self.collectionView indexPathsForSelectedItems];
NSIndexPath *indexPathImInterestedIn = [arrayOfIndexPaths firstObject];
//Voila
There is no method indexPathForSelectedRow: in collection views because collection views are not limited to rows. You can have rows, columns, circles, spirals, rows AND columns, or an almost limitless list of different ways to arrange cells.
Do a search in the UICollectionView class reference for methods who's names begin with "indexPath".
The closest match is probably:
indexPathsForSelectedItems:
That method works for single or multiple selections. It returns an array of indexPaths instead of a single indexPath (much like the table view method indexPathsForSelectedRows for table views that support multiple selections).
There are also methods indexPathForCell:, indexPathForItemAtPoint:, and indexPathsForVisibleItems.
I want to get a NSString out of the table HeaderView that's on top that moment. Because I want to implent it in ScrollViewDidScroll so one label changes when the header view changes.
Didn't find any clues on the web how to do this
Thanks!
There is no predefined method by which you can get top section. There is one trick by which you can achieve it. First get indexPath for all visible cells.
NSArray *visible = [tableView indexPathsForVisibleRows];
NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];
Now you can get section value by indexpath.section. After getting this section you can get string from your data array for this section.