Reload Content of UICollectionView Without modifying its datasource - ios

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".

Related

iOS / Swift 3.0: how do you determine which rows are currently visible in a UITableView?

How do you determine which rows are currently visible in a UITableViewController in Swift 3.0?
You can use:
tableView.visibleCells
This is an array of UITableViewCell that are currently visible in the table view.
If you just need the index paths of the visible rows, you can use:
tableView.indexPathsForVisibleRows
which is an array of IndexPath objects.
It's quite simple. You just need to use…
tableView.visibleCells
From Apple docs…
The value of this property is an array containing UITableViewCell
objects, each representing a visible cell in the table view.

UICollectionView NSIndexPath?

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.

Adding one row to UITableView with a data source

I have a UITableView that loads certain data from a remote feed, and I want to manually add one row on top the table, at the beginning of all the rows loaded from the feed.
So for example, if from the feed I would have a table with the rows
//////////////////////////////////////////
1 (loaded from UITableView data source)
//////////////////////////////////////////
2 (loaded from UITableView data source)
//////////////////////////////////////////
3 (loaded from UITableView data source)
//////////////////////////////////////////
I would like to manually add one row on top of those, so I would have:
//////////////////////////////////////////
0 (manually added)
//////////////////////////////////////////
1 (loaded from UITableView data source)
//////////////////////////////////////////
2 (loaded from UITableView data source)
//////////////////////////////////////////
3 (loaded from UITableView data source)
//////////////////////////////////////////
Is this possible? Is there any other way of doing this in case it is not possible? Like having two UITableView and putting them together or something like that.
Thanks a lot in advance!
Yes it's possible.
In the function where you want to add you row, do :
NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
you can change the row insertion animation depending of what effect you want.
/!\ your data source and your function tableView:cellForRowAtIndexPath: must be consistent with what you want to display
Add the new element to the array where you have the data, for example:
//This is your data:
NSMutableArray *myData;
//Add new row at the beggining:
[myData insertObject:newData atIndex:0];
//Reload your table view
[self.myTableView reloadData];
If it's static first row you want to add, I would suggest you use a UITableViewHeader to display it instead of UITableViewCell. If you do want to it to be a sell, why don't you add another section? The first section will contain just this one cell, and the second will contain all the cells displaying the data you get from the feed. You can skip the header view, so it will not be noticeable that you have 2 sections instead of one.
Another way is of course to add the data you want to display in the first cell to your datasource and have just one section, but I like the first approach better, because there the code and logic is cleaner
It may useful to you..
1) Insert first that new element into your datasource array.
2) Get visible row indexPath by
NSArray *visibleIndexes = [self.tableview indexPathsForVisibleRows];
3) Then reload all visible rows without animation :
[self.tableview reloadRowsAtIndexPaths:visibleIndexes withRowAnimation:UITableViewRowAnimationNone]
You must be using array to load the data from remote in your table view.
Just push a element(whatever data you need in first row) in beginning of those arrays.
Insert an element in the beginning of array as:
NSMutableArray* array;
[array insertObject:dataInFirstRow atIndex:0];

hide empty rows in uitableviewcell

I am displaying files in a tableview. In cellForRowAtIndexPath, I want to display only certain types of files.
I am doing something like this:
if (document is folder) {
display folder details;
}
else (document is not folder) {
display empty cell;
}
by doing this i can display empty cells but those are still there(hidden) how to get rid of those rows. see the pic for details.
I think you should rethink the problem, cellForRowAtIndexPath: should never be called for a cell that does not exist. Determine how many folders are available in your data set and return that number in tableView:numberOfRowsInSection:
Then you don't need to worry about hiding it once cellForRowAtIndexPath: is reached because it is a real cell that should be created.
To 'hide' cells you will need to first create an array of only Folders. Your not really going to hide cells, but rather only include the correct objects in your tables datasource array.
In your viewDidLoad, iterate though your entire documents array and add any Folders to a separate folderArray like so:
_folderArray = [NSMutableArray new];
for(Document *doc in documentsArray)
{
if(doc is a folder)
{
[_folderArray addObject:doc];
}
}
Use that folderArray in your all of your tableView delegate & datasource methods instead of the documentsArray.

How i can change the content of a UITableView when i press a button

i want change the content of a UITableView when i change a button, how i can do it?...i retrieve my information from Core Data, and when i press a button i want change the information in the tableview, i know the:
[self.tableview reloadData]
and in this way i give the information to the row:
NSManagedObject *name = [[self sortName] objectAtIndex:indexPath.row];
cell.textLabel.text = [[name valueForKey:#"firstName"] description]];
but my question is how i can give another array to my table view to retrieve information when i tap a button...i every where but i can't find an answer...
The information displayed in your table view cells are determined in the cellForRowAtIndexPath method.
In your code, it looks like this method references your array named sortName.
If you are retrieving your new data set into some other array, then you have to replace the contents of sortName with the contents of the new array.
Replace your sortName array with the new contents after pressing the button. Than call reloadData..!?

Resources