Why use UITableViewHeaderFooterView or tableHeaderView instead of multiple UITableViewCell? - ios

Question
What is the benefit of using either tableHeaderView, tableFooterView or UITableViewHeaderFooterView as opposed to just using multiple UITableViewCell, and customising it to look like a header or footer?
Example
A static banner image
A static part with input fields that all look the same:
First name
Last name
Age
Height
Weight
A static part "Appointments", which can be expanded
A dynamic list of doctor's appointments
A static part "Images", which can be expanded
A dynamic list of images
A static part with a "Save" button
My gut reaction was making the top two static parts (1, 2) the tableHeaderView, the other static parts (3, 4) a section header, the dynamic lists (under 3,4) to becells inside sections, and the Save button the tableFooterView.
But then the input fields (under 2) look similar, so they could also be cells.
But then I need to treat sections in a special way, because the first section does not have a header view.
And then, 2 months later, the requirements change and 5 other special parts are added.
So all in all, I'm wondering, why not just implement everything as a UITableViewCell, and have a CellType enum that can easily be extended?
Then you have a flat array cells: [CellType] which is the single source of truth for the table view, and a single method func buildCells() where you populate this array.
So far, this is the only article I've found that talks about this, and I'm kind of agreeing with it.

This approach (just tableview cells, no header/footer views) works fine. In your example, I would have made all of these cells, too.
What is the benefit of using either tableHeaderView, tableFooterView or UITableViewHeaderFooterView as opposed to just using multiple UITableViewCell, and customising it to look like a header or footer?
While a series of cells might make sense in your particular example, I wouldn’t generalize that conclusion and, therefore, categorically dismiss the idea of header/footer views. There are many cases where the header/footer view approach is most logical and intuitive:
in many cases, the section headers don’t have any model data associated with them, but are just for organizational purposes ... in this case (e.g. grouping a list of contacts by the first letter), the header view approach makes a lot of sense;
in other cases, we’re dealing with arrays of arrays of homogenous items ... again, the header view approach makes lot of sense; it has the benefit that the index path section and row now map very nicely to the model;
sometimes we want a header to persist as it scrolls out of view (so the user can see what section they are in even though the header would have otherwise have scrolled out of view);
sometimes we want a footer that shows some derived, calculated value (e.g. when listing items on an invoice, perhaps the “total” should be shown in a footer).
Your scenario feels more like a heterogenous list of model objects. In that case, I think using cells for this model data is perfectly reasonable. But obviously header and footer views still have utility and, in many cases are simpler and more intuitive.

Related

Where is supplementaryViewOfKind used?

I'm trying to figure out where supplementaryViewOfKind is used. I looked into the documentation and some other questions on SO, still I wasn't able to understand what it does. From my understanding it's to be used as an auxiliary Headerview.
collectionView.registerClass(imageCell.self, forCellWithReuseIdentifier: "imageCell")
collectionView.registerClass(imageCell2.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "imageCell2"
Can't I alternatively have imageCell2 registered this way:
collectionView.registerClass(imageCell2.self, forCellWithReuseIdentifier: "imageCell2")
and then when I need, at cellForItemAtIndexPath I switch cases for the which cell to present? or again am I not understand the existensial purpose of supplementaryViewOfKind
You'd better check this article:
Collection View Programming Guide for iOS
In the section Custom Layouts: A Worked Example, you can find an example of custom layout using Supplementary View.
In the example above, connecting lines are implemented using Supplementary Views.
You can use Supplementary Views in various ways with your custom layouts.
The shortest answer
Watch this video:
https://developer.apple.com/videos/play/wwdc2012/205/
The short answer
The supplementary view, like its name, provides supplementary material that needs some data to be displayed properly. Cells and supplementary views can be closely related to each other or have little to do with each other.
The concept is a little different than UITableView so you might want to read below how it is different. In short, UICollectionView is intended so people can have a group of/groups of views laid out in a non-linear way, according to their own rules.
Example
Let's say I made a map using UICollectionView. I could use UICollectionViewCells to display buildings.
Here, I can use the supplementary view in infinitely different ways.
I can choose to use it as a label to display the name of town. In this case I would be using one supplementary view for one section.
OR I can use supplementary view to display the name of each building. In this case I would have the same number of supplementary views and cells.
OR I could actually do both, registering different classes to the same collection view.
UICollectionView vs UITableView
It's easy to get confused because the terminology used for UICollectionView is similar to what is used in UITableView, but it's crucial to know the differences between the two in order to understand what things do in collection view.
a) It has a layout object whose main concern is the display of cells registered to the collection view. As far as I remember the stock UICollectionViewLayout does nothing, or provides minimal layout functionality whereas the UITableView class has a built-in way of laying out its cells--i.e. a linear vertical row.
b) You define the rules through the layout object. This rule governs how things--cells, supplementary views--are displayed in the collection view.
c) Because the collection view is intended so you define a custom layout object, so as to display a set of objects(i.e. cells, supplementary views) following a certain rule (i.e. the rules defined by the layout object) that aren't laid out in a linear manner, there's no real sense of "section header" in a collection view. A section header in a table view is a view object that provides the title or summary of what is in the section. A supplementary view doesn't have this "one per section" limitation. You can use it like a section header if you want, but you don't have to.
d) As mentioned in the example, you can register many different kinds of supplementary views. It's not like the table view where you only get a header and a footer.
e) The section and item in the IndexPath are just logical groups. (Notice UICollectionView doesn't use the term row, since it doesn't assume cells will be laid out in a row) Cells in a section don't even have to be laid out close to each other if that's how the layout rules are defined in the layout object.
f) The index path of cells and that of supplementary views have no connection with each other unless you decide to logically group them together. You can have cells but no supplementary views, the same number of both, or more supplementary views than the cells. They are two different discrete entities unless you choose to logically group them and use them together.
g) In case people want a somewhat linear but different from UITableView look (e.g. a horizontally scrolled table view or 4x4 layout of pictures), Apple provides UICollectionViewFlowLayout, a subclass of UICollectionViewLayout. Using this you can have the table view like behavior, as well as the header and footer as supplementary views.
I'm trying to figure out where supplementaryViewOfKind is used
A layout can define supplementary view kinds however it likes, to give you a way to specify things that are not ordinary cells (items).
A good example is the way UICollectionViewFlowLayout lets you ask for section headers and footers. They are supplementary views. In particular, they are supplementary views of kind UICollectionElementKindSectionHeader and UICollectionElementKindSectionFooter. See the documentation.
In my Latin app's list of chapters and sections, the chapters (black bars with white numbers at left) are supplementary views:
The supplementaryViews are the Paris and Chamonix in the image Below.
They are neither the header, nor the footer nor the cell. They are something else. Similar to cells you would have register them and then also dequeue them, or that they also do take data from the dataSource so in that sense they are data driven, but they just don't contain data for the cells, they contain meta data...
Just jump to minute 5:47 of this WWDDC video. Though I suggest you to see the entire video from beginning.

How to wrap self sizing UICollectionViewCell

My goal is to have a Swift implementation of a UICollectionView with dynamic content where each cell hold parts of a sentence. Each part of the sentence can be edited by the user. The problem is that some parts of a sentence might be longer than the container itself. Using sizeToFit is not an alternative because all content should have the same font size to maintain readability throughout the collection view.
Right now the behaviour I get, when I have a part of a sentence longer than the container is the following:
As you can see the third row has leading ellipsis.
What I would like to achieve is the following, the overflowing part of the cell should wrap as analogous to a span tag in HTML, like so:
Is this possible? How can I achieve such a thing?
As far as I can tell, it's possible but complex - here are a few pointers of what I'd say you're going to need:
2 additional collection view cell types: in the first, only the left edges are rounded, in the second only the right
Use Core Text API to measure where your text needs to be wrapped
In your datasource, you're going to have to then recognize the situation before you create the cells and then instead of creating a single cell, you create two, using those new types you have.

When to use UICollectionView instead of UITableView?

I found that UICollectionView is like an upgraded version of UITableView introduced in iOS6, but when should I choose UICollectionView instead of UITableView?
There are still Apps using UITableView, if UICollectionView can do anything UITableView can do , why people still use UITableView? Is there a difference as far as performance is concerned?
Thanks!
That depends on the requirements. How the application flows determines which type of UI to integrate into the application.
People mainly use the UICollectionview for creating types of UIs with multiple images shown in a grid. This would have complex logic using UITableView, but with UICollectionview, it would be easy.
When using UICollectionview, you don't need to set buttons with tags or other things by getting selected items values. You can simply get -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath and in UITableViewDelegate:
`-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`
You get the selected row instead of the item, so for creating grid or modified items, using UICollectionview is best.
For the listing details of each item, people use UITableView because it shows more info on each item.
Apple Docs:
UICollectionView Class Reference
The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts. Collection views provide the same general function as table views except that a collection view is able to support more than just single-column layouts. Collection views support customizable layouts that can be used to implement multi-column grids, tiled layouts, circular layouts, and many more. You can even change the layout of a collection view dynamically if you want.
UITableView Class Reference
A table view displays a list of items in a single column. UITableView is a subclass of UIScrollView, which allows users to scroll through the table, although UITableView allows vertical scrolling only. The cells comprising the individual items of the table are UITableViewCell objects; UITableView uses these objects to draw the visible rows of the table. Cells have content—titles and images—and can have, near the right edge, accessory views. Standard accessory views are disclosure indicators or detail disclosure buttons; the former leads to the next level in a data hierarchy and the latter leads to a detailed view of a selected item. Accessory views can also be framework controls, such as switches and sliders, or can be custom views. Table views can enter an editing mode where users can insert, delete, and reorder rows of the table.
Here's my criteria:
If a UITableView can do it, use it
If a UITableView needs lots of code to do it or can't do it at all, use UICollectionView.
You have to consider the restrictions on UITableView before making a decision: It's a single column. And you can only customize the cells, but not section backgrounds and such. So if you have a straight-up list of things with no extra frills - that looks like a bog standard iOS view, basically - then use UITableview. If you have custom insets, or a border around each section, use UICollectionView.
I'm actually considering UICollectionView for all things simply because it's very expensive when you start developing your view as a table view, then later find out it can't do that one thing that you need it to do. 1st hand experience ;)
Edit after even more experience with the two: Disregard that last paragraph. UICollectionView requires a lot of boilerplate code to make it work like a UITableView. Use UICollectionView only when really needed. ;)
For simple lists and forwards/backwards navigtaion, use UITableView.
If you need a high degree of customisability, use UICollectionView.
Generally speaking, in software development, it's best to choose the approach which represents "The Simplest Possible Thing".
EDIT: As of iOS 14, UICollectionView can now do lists as well and is now the recommended approach. See this session from WWDC20 for more information and implementation details: https://developer.apple.com/videos/play/wwdc2020/10026/
According to my point of view main difference between collectionView and tableView is that
TABLEVIEW --> show list of items in only one column.
COLLECTION-VIEW -->show list of items in multiple column.
Hope it will help you.
If you choose UITableView for iPhone, make sure you have considered your iPad strategy first. If you want an iPad-specific layout, you may want that single-column layout to become a grid.
Although it's not required, I always use a collectionview. That way I can easily adapt how my collections are presented for differing resolutions. A plus is that it's ready to quickly add new types of cells when refactoring in the future.
I see no point of tableviews. It's very simple to use a collection view to represent a table. IMO.
From my personal experience the two elements should only be compared loosly.
TableView
A TableView is a UI element designed for showing data in a list format. There is certain functionality that comes as standard with a UITableView, such as:
Accessory View
Cell Selection Style
Editting Style (Delete and edit buttons).
The above elements enhance the usability of data when displaying and interacting in a list format. Such as viewing emails.
CollectionView
A CollectionView is a UI element designed for showing content using a custom layout (usually anything that isn't a list). CollectionViews improve functionality of displaying data in completely bespoke layout styles and also dynamically changing layouts on the fly. Some examples are:
Horizonal Lists
Photo Galleries
Thumbnail views
Carousels
Dials
Laying out elements on a map
etc.
CollectionViews also allow for multiple selections.
Conclusion
As you can see from the above, both have completely different use cases and are designed for enhancing the development and usability of their own specific data sets.
If you are looking at displaying anything in a list style with the followin interactions:
- Adding
- Deleting
- Re-ordering
Then a UITableView will simplify this process by providing the support straight out of the box.
Anything else, you should leverage the benefits of CollectionView as you have more flexibility.
Its totally dependent on how your data to be shown.
As mentioned by many above, if you require only single set of data and that too not complex, go for UITableView else use UICollectionView.
UICollectionView is customization friendly.
If you are dealing with multiple cell heights or so, then go for UICollectionView.
Both are depends on the requirements. Table Views also have support for a variety of editing scenarios. This support has not been implemented in the Collection View classes.
If you are converting from a Table View that relies on these methods, expect to do a little extra heavy lifting in the Collection View.
Collection View section headers can be placed anywhere within the view.
and UITableView don't need to set buttons with tags or other things by getting selected items values.
In practice, everyone uses UICollectionView that I've come across, when they only need a UITableView. "It's one-dimensional. It goes up and down. Why are you adding unnecessary delegate methods for layout AND data?". I once spent an extra 2 hours helping a startup find out why their UICollectionViewCell got squished because the owner, who didn't read the Animations manual, nor HIG, nor the UICollectionView guide, decided to use it and add variable heights and anims. Needless to say, he gave himself a headache and much lost time on a non-business-critical issue he could have avoided by simply using a table cell, since there's no extra layout delegate + Nib.
Let me get this straight, I am all for UICollectionView's when your data and display need it. They're very powerful. But in practice, most people I've seen have been using them on lists.
This brings up another flaw. They're also used on short, constant lists that won't change, ever. In this case, just make a Xib. Or write a custom view that stacks them. Why? Because you don't need the memory management for 5 sets of labels with a button or switch. If they might change, then yes, use a list. If you want physics, then UICollectionView works well with a some cool effects. But do you really need to add 5 delegate methods and a layout system for 5 labels that will never move?
Also, I'm not forgetting that iOS has a native stacking view now too. I can never get it to deform how I want, even though I'm quite adept at the 2D and animation systems, so I never use the built-in one.
All I'm saying is, define your requirements. Maybe you don't need either of these, if your UI isn't adding/removing items and refreshing itself. Or maybe you want to write a Card Game and throw them out virtually on a table, then use UICollectionView with a physics system for its layout guide.
Personally I think the UICollectionView can do most of the work which UITableview can do. well, at the same time, it's more complex to use.
I suggest you use UICollectionView as TableView just in case your manager change requirements in the future.
Based on our need we are choosing TableView or CollectionView.
Example:
For phone contacts tableView is best option.
For photo gallery, collection view will be best option.
I had this issue in my current project. Which to use. In my case it was simple really. I needed both. I needed my view to look like UITableView and also to change its change / layout. So, UICollectionView was used. I also use UITableView everywhere I don't need any extra customisation. Since UiTableView comes with a default layout that includes images and text - I use it for simplicity.
Based on our requirement we choose UITableView or UICollection view.
If we want to display images or items in grid type or if we need more customisability we use UICollectionview.
For listing each item with details and subdetails we use UITableView.
UICollectionView:
The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts. Collection views provide the same general function as table views except that a collection view is able to support more than just single-column layouts.
UITableView: A table view displays a list of items in a single column. UITableView is a subclass of UIScrollView, which allows users to scroll through the table, although UITableView allows vertical scrolling only.
As per my view for Grid View display use UI Collection View.All other list view use UITable View

Using deep index paths in UICollectionView data source

I am trying to back my UICollectionView's data source with a data structure that (for good reasons) is more than two layers deep. In other words, my data can be broken into sections, but not all of those sections simply contain items. Some contain arrays of items (or "nested sections", so to speak). These items can be easily be referenced with an index path three indices long (3.2.4, for instance), but UICollectionView's data source methods only seem to support index paths up to two indices long.
All of the UICollectionView APIs use NSIndexPaths to reference collection view items, so in theory they should work with any number of indices. The challenge is getting the collection view to "know" to ask for cells, layout attributes, etc. for items at these deeper indices when the data source protocol only provides collectionView:numberOfItemsInSection: and
numberOfSectionsInCollectionView: to specify the indices it needs to ask for.
Is there any way around this that doesn't require me to (a) subclass UICollectionView, which Apple explicitly discourages (second point under "Tips for Implementing Your Custom Layouts") or (b) flatten my data structure, which would be less than ideal, and which seems unnecessarily limited given the APIs UICollectionView already provides?
It doesn't make logical sense in the scheme of a standard collectionView (or tableView) to have a three tier index. I've seen some cool tableviews that have "expanding" cells that work in the same way you're describing. It's just that the object representing the expanding cell tells the table that it has multiple items available.
I can't understand how your collectionView is supposed to look as an end product. If you are encountering arrays at some index paths, what are you trying to represent? I don't want to inform your design but why not just show one item at that particular index, and when a user selects it, expand or show another view with those subsequent items?

"Compact" view in iOS

I have a view in iOS (iPhone) that have multiple components, organized in sort of a stack way (one in top of the next). Those are user account properties, some could be blank.
So, I have in my view the components layout like this:
UITextField1 (Name)
UITextField2 (Location)
UITextField3 (Age)
UITextView1 (Bio)
UITableView (user entries).
Some of the fields could be blank. Instead of having blank spaces for the blank fields I would like the next field to move upper.
This is like this question of flowlayout: What is the best/easiest way to create 'flow layout' type layout in iOS.
I can only see two ways of dealing with this:
Creating a function that traverse all the UIViews and determines which ones are blank and move the following upper.
Creating a UITableView and use different cell heights for cells whose content is empty.
Ideally there would be a component, but I cannot find it (basically some sort of stack/flow layout).
Anyways, I believe that I am going to implement the option #1 above, but I don't know if there is an "standard" way of accomplishing this (I honestly don't even know the proper term to look for this feature).
Thanks.
I would lean more in the direction of your second choice by using a UITableView but not the way you propose.
This would be my approach using a UITableView:
Create a UITableViewCell (or custom cell) for each one of my
components and assign a tag value to each, we'll use these later. You can do this in viewDidLoad.
Add code in numberOfRowsInSection to check to see which fields have
data values present. Return the total count of the number of fields
with data values
In cellForRowAtIndexPath again, check if data exists for that field
If so, check to see if the cell created in step one has already been created or not (if not, create)
If not, increment a counter of some sort to increase your tag value and find the next field that has a value. Once found, use that "tag/index" number to "return" the proper cell.
In the end, you have a UITableView only displaying the fields with data.

Resources