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.
Related
I've been using the code below to access the value of a textfield in my custom UITableViewCell. Problem is, this only works for cells that are visible at the time the method is called.
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:3];
AppointmentNotesTableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
NSString *str = cell.notesView.text;
Does anyone have a better way to access this information, regardless of the cell being visible or not?
That is because invisible cells do not exist. Cells get reused when you scroll the table view. Only those of them, visible on the screen can actually be accessed.
All the objects that potentially displayable in the table view are normally stored in some kind of list. What you need is to access the object from the list using the index you have from #path instead of trying to access its "rendered copy" from the table view itself.
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
Let's say I have a UICollectionView that displaying array of UIImages and the user can select multiple images. So my question is how can I reload the UICollectionView to let it shows only the selected indices without generating a new array that contains the selected UIImages (indices).
First of all you need to Get the images selected with value in array and then use that array as DS and use
[self.collectionView reloadData];
Individual sections and items can also be reloaded:
[self.myCollectionView reloadSections:indexSet];
[self.myCollectionView reloadItemsAtIndexPaths:arrayOfIndexPaths];
I would use an array of ImageObject which contains an UIImage and a BOOL isDisplay for each item. So we don't have to use an another array but a check the variable isDisplay for the "cellForRow".
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.
I have a view with a map and and list displayed at the same time. I would like the map to move when the user scrolls the list.
I was hoping for something like
(UITableView*)tableView didScrollTableViewCellToTop:(UITableViewCell *)cell
I haven't found something that will support this yet.
Thanks in advance
You can get all the visibleCells by running...
NSArray *cells = [self.tableView visibleCells];
This returns an array of UITableViewCells.
You can then find the one with the lowest indexPath.row value to find the top one.
Remember that the UITableView is simply a scroll-view. So you can get the offset of the scroll view and use the position to get the cell at that location:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: point];
To continuously monitor and change, implement a scroll-view-delegate and implement the scrollViewDidScroll method.