UICollectionViewCell not displaying in UICollectionView - ios

I am new to iOS and just trying to build a template view for future use.
I designed a view in Main.Storyboard with a UICollectionView - with several (hardcoded, I guess?) UICollectionViewCells. The UICollectionView displays when I test the app (background of that portion changes color), but I am not sure how to get the UICollectionViewCells to display.
All I have done so far is connect the view to the appropriate view controller, but I am very confused as to where to go from here.
My Main.Storyboard : http://imgur.com/a/aXy3O
If possible - is there a way to quickly add a function to trigger an alert when any UICollectionViewCell is clicked?
Thanks.

UICollectionView have very similar structure like UITableView.
So if you check its delegate methods you will find what you need.In this case you need to implement this method:
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath

Related

Is it possible to go to the next page using Collection View and display data?

I'm the middle doing some project that involve CollectionView and property list. I want to do the image grid in the Collection View, and the image is the significance to proceed to the next page.
Here I want to do:
1) User click image in the collection view
2) Image will redirect to the next page and display the data that stored in the property list. The data is display according to the image that they click.
I'm using Xcode 6.
How can I perform such process?
Here is the almost similar example give by Apple. It will give you a good point of reference.
UICollectionView has a delegate which provides a method
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
When an item is tapped, the function above is called. Based on indexPath you can determine which item was selected.
In your storyboard, create a new UIViewController and create a segue from your UICollectionView to this UIViewController and make the segue as Push and name it 'collectionViewToUIViewController'.
In your didSelectItemAtIndexPath, call method
[self performSegueWithIdentifier:#"collectionViewToUIViewController"]
Also implement prepareForSegue where you tell the UIViewController what to display.

How to create a view for settings of my iPhone apps in Xcode storyboard

I am new in iOS programming and I would like to create I view for the settings of my app that looks like iPhone's one (like in the picture). Some of rows will call other views when taped and other will not.
What is the best way to do that? I thought about TableView with prototype cells, but is there a best way to do it? or a best-practice for what I want to do? Or maybe a tutorial online?
The fast way in Interface Builder:
Use a UITableViewController, make it STATIC and use the GROUPED style (all in IB).
You can setup the cells to show disclosure indicators (or not) in IB also.
You can segue directly from the rows or the UITableViewController to where you want to go.
If you segue from the UITableViewController, implement the "didSelectRowForIndexPath" method and call "performSegueWithIdentifier" accordingly.
A structure like this is best by UITableView.
First you select how many sections you want, and customize each section with a data structure that you have to be filled with (Probably an array.)
Then you fill up each rows inside
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method, and call your value from the array/dictionary that you have.
for going to a next view when clicked upon
Use the method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Hope this helps
The best way to do that is using static UITableViewCell.
See UITableView Programming.
The optimal solution here is undoubtedly UITableView. This is because firstly, you have the need to display a list of options that would have external links to other pages and UITableView is designed and used for this purpose.
In an addition to that, if you want, you can also expand and collapse the rows of your parent TableView into a Child TableView i.e a UITableView as a subview of its parent UITableView.
Put up a UITableView and populate it with UITableViewCell. That will be just fine with the requirement you have.
Hope this helps.

How to implement ViewController with CollectionView inside?

I am trying to implement a "profile" page in my app which is similar to Instagram profile page
But the problem I have is the Cell from UICollectionView below get loaded by -collectionView:cellForItemAtIndexPath
But it load every cells at once ,it does not just load the visible cell as it should. And this is because it is inside scroll view ,the make every cell loads immediately.
The image for cell will be sent from server via API and I want to do a loading when user scroll to 2/3 of page. And this can be done easily in UICollectionViewController.But I want to know how to do that in ViewController that contain collectionView in lower path.
Please help. I think using Header of CollectionView as an upper part of page might solve the problem. But I also want to implement that when user tap the button and then the display below change ( just like instagram page). So I am not sure what direction should I go . Should it be a custom container VC or something else ?
TIA
UICollectionView is very versatile. It can handle the entire view, including the upper part that pertains to the user's profile.The trick is to make use of a "supplementary view" in collection view terms (like a header view in table view terms).
I suggest that you throw away the scroll view and make the collection view do all the work for you. Just like you register classes for cells, register a view for the user profile portion of your UI using:
- (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier
And respond to the datasource method:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
By answering the that user profile view.

How to implement UICollectionViewCells showing "stateful" contents?

I am currently refactoring my Newsstand app.
Previously, I was showing several custom UIViews ("CoverView") in a self-calculated grid layout (3 items per row, as many rows as necessary) embedded in a scroll view, scrolling vertically.
Each "CoverView" consists of
a label (showing the issue title)
an image (showing the issue cover)
a button (showing "DOWNLOAD" or - if already downloaded - "READ")
a progress bar (initially hidden, showing the download progress)
Each CoverView has it's own UITapGestureRecognizer and UILongPressGestureRecognizer.
If a user taps once, the button is hidden, the download progress bar is shown and constantly updated with the download progress:
[issue addObserver:cover forKeyPath:#"downloadProgress" options:NSKeyValueObservingOptionNew context:NULL];
(The long press recognizer is used to let the user delete the issue).
All CoverViews are stored in a NSMutableDictionary, and are always kept in memory. Although the unscaled images are rather big (1024 pixels high), I never had performance problems while scrolling, probably since there are not that many issues (and therefore CoverViews) to store.
Since the self-implemented grid-layout is complex and difficult to maintain, I wanted to use a UICollectionView instead. I added a collection view and made my CoverView class inherit from UICollectionViewCell (instead of UIView).
The display and scrolling works, but I'm having these problems:
The scrolling is lagging when a new row of cells needs to be shown and collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath is called for each.
I tried to not reuse cells (to improve performance, strangely enough), but this failed due to each cell having to have it's reuse identifier set (and the "reuseIdentifier" property is readonly, so I cannot set it in code).
I don't know where to unset the observers for KVO for a cell. Is there a way to be notified once a cell gets queued for reuse?
In general, I'm unsure whether I'm misusing UICollectionView with my specific use case here and should stick to my previous approach, or whether it's just my inability to efficiently use and tweak UICollectionView.
To set 'reuseIdentifier', the proper design is to use UICollectionView methods :
registerClass:forCellWithReuseIdentifier:
or registerNib:forCellWithReuseIdentifier:
You typically call these methods once, depending on how you want to provide your cell's instantiation (inflated from NIB, or instantiated through standard [[ViewClass alloc] init]
Once views are registered, you can use them from UiCollectionView like this :
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"yourReuseId" forIndexPath:indexPath];
// Collection view handle ALL the work :
// 1 - retrieves a reusable view if views have already been recycled
// or 2- instantiate a new one, using the class/nib you registered
// So, cell SHOULDN'T be nil from here :)
// Note : from iOS 6, UITableView has a similar auto-instantiation mechanism
}
3 - to unset KVO, override UICollectionViewCell's -(void)prepareForReuse method (inherited from UICollectionReusableView)

XCode 4.4 remote Image in a Custom Cell Table View

I'm new in Xcode Programming and I'm developing a small application but I have a problem.
I have a Table View with a Custom Cell, taking externally the data to populate the view (by JSon). I have a big slow navigation in list because in the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. I get an external image and this makes it all slow because it runs every time a cell displayed.
How can I populate the entire list without using this method? Or is there an alternative to make it understand that the image exists?
There are few methods Apple suggests when customizing your UITableViewCell. You probably is trying to add subviews to the contentView of the cell. A better approach is described here Subclassing UITableViewCell. Well, I suggest you read the TableView Programming guide.

Resources