iOS: Using custom UITableViewCell vs. Using Cell viewWithTag? - ios

Custom Cell For UITableViewCell
I'm developing an iOS application and i need to make a custom cell for my side menu, i have seen so many examples and i found out those two approaches :
Make a class (.h and .m files) that inherits from UITableViewCell and put the outlets in the .h file then apply the class on the cell and connect those outlets to the labels and/or images from your cell in the storyboard.
Or the easy way is to give a tag to the labels/images or whatever you have in the cell in your storyboard.
My Question: I'm wondering which way is more accurate and professional and used by most iOS developers ?

Definitely the first one. It's clear and maintainable in the future. The purpose of the tag is not to store arbitrary values. See my answer regarding this.
This article has some details on the usage of tags

Related

How to make a super class of custom UITableViewCell?

I am creating a UITableView which has multiple types of cells.So I creating multiple cell using xib and for each cell I have one .h file one .m file and one .xib file.All these cells has some common things like cell background color,property(UILabel,UIView) and actions(UIButton click).
So I am doing a set of common things again and again.So how can I create a super class of these cell so that I can come out of the problem.I should be linked the custom xib cell to my super class.
Edit 1.0 :-
Suppose I have created a Super Cell subclass of UITableViewCell having the all the common properties of those cells in its header file (SuperCell.h) and also implemented all the common actions in its implementation file (SuperCell.m).Then I Made all those .xib cell header file as a subclass of my SuperCell. Now how can linke these .xib files header property to the SuperCell header property which is same.
Edit 2.0 :-
Thanks #Fogmeister for pointing me out that it will be a big hierarchies and difficult to maintain.And If I want to add some new label in child cell then I am also not clear where should I add and how to linked with the super cell.
Let me clear my question explaining my project a little bit.
I am creating an social app like facebook which has Text post,single image post,double images post,multiple images post, poll,event,etc.
So for my social app landing page I have a UITableview controller and all these type of post is linked to one one cells.All these cell has some common things like Post ownername(UILabel),#handler (UILabel),profile pic ( ImageView) ,Post time (UILabel),like button (UIButton),comment button (UIbutton) etc.
I have done everything and it is working fine.I have written a lot of common code for setting up all these cell as there is not SuperCell of these cells.So I am trying to figure out a solution to make it little bit easier.
Thanks
Ah, I see your problem now. As a very first starting point I can think of two possible (maybe three) ways of approaching your issue.
(N.B. everything here is just me using the Facebook app as an example, your actual app may differ).
At the moment you have different cells StatusCell, PhotoCell, VideoCell, ShareCell, etc...
Each of these have various different elements... userNameLabel, userAvatarImage, timeLabel, likeButton, commentButton.
Then each has a "contentArea" that contains the status, photo, video, url, etc...
First solution - Component views
The first approach I was thinking is to keep your different cell types but then to create UIView subclasses to easily populate the areas. So instead of the cells having the different user labels and images etc... create a view called UserDetailsView.
This UserDetailsView will take a single property of a User object. It then uses this object to populate the different labels it contains such as userNameLabel, userAvatar etc...
Now you can just add this view to each different cell type.
You can also create components for the ShareView which might include likes, comments, etc...
Second solution - Generic cells
In addition to creating these different components for each different type of Cell you could actually use a single type of cell. (This would only work if the content is in roughly the same place for each).
So the additional part to create now are the different content views. This might be a StatusView, PhotoView, etc...
Now you can use one generic cell type that has a space for a content view. (Maybe placed inside a container view for positioning and constraints).
Third solution - React Native
What Facebook does for their timeline is to use the React Native framework that they have created for immutable view hierarchies. This is a more complex method as it requires reworking the way you build stuff but definitely one to keep in mind for the future.
https://facebook.github.io/react-native/
create your "parent cell":
#interface SuperCell : UITableViewCell
#end
#implementation SuperCell
// background logic and all the stuff that is equal for your child cells
#end
with it's .h and .m files.
then create your "child cells" (with their .h and .m files) and make them inherit from your "parent cell":
#interface SomeCustomCell : SuperCell
#end
#interface AnotherCustomCell : SuperCell
#end
and so on...

UITableView with 8 inherited cell type

I have a UITableView, with 8 differents type of cell. Those custom cell have some design in commom (like a upper left icon, a title, a subtitle...). However, under those common features, each cell is different. In order to have less to maintain, my 8 cells inherits from a default abstract cell with the commons IBOutlet.
Now my question is : what is the best, most proper way to do this?
At first, I thought of using registerNib:forCellReuseIdentifier: but this means 8 different xib. In this case, if one of the common design feature change, I would need to go trought all 8 xibs to change the same thing. I thnik it's not very productive and clean.
I thought also of registerClass:forCellReuseIdentifier: but this methods does not create the cell with a nib, so I would need to do everything programmatically.
The solution could be have one common xib, different registered class in the tableview, and those will be responsible for using their own custom design. But I can't see how to achieve this with those two previous methods.
I would use a single nib file with 8 different UITableViewCell subclasses. You should be able to use registerClass:forCellReuseIdentifier: and inside the class (which subclasses UITableViewCell) you make use of the nib file. If it is not clear I'll try to provide some code.

In iOS can a cell in a tableview have more than just a text

Can i have two images and a few text labels in a cell of a tableView ? All dynamically assigned ?
Yes, read the documentation for the UITableViewCell class, specifically the UITableViewCellStyle constants which allow for various types of standard table cells to be created. If the built in styles do not meet your requirements, you can subclass UITableViewCell and create anything you want, since it's just another type of UIView.
Of course you can! There are several ways of achieving that, the most popular being subclass the UITableViewCell class, and creating your own custom cell, and you can have whatever you want in it. But just to get an idea of how to go about doing that, I suggest you go through the following links:
Table View Programming Guide for iOS - Apple
Crafting Custom
UITableViewCells - MobileTuts+

Custom UITableViewCell using storyboard with some cells

I have a TableView in which a most cells are pretty standard. I make them buy using static cells in Storyboard. However, one cell I would like to customize probably using an XIB file so I would need to load it programmatically.
In the TableView's data source, is it possible to handle loading XIB view for this particular cell only, while leaving other cells to what's delineated in the static cells in the Storyboard? Or is it an all or nothing thing where I need to just give up using static cells altogether?
The rationality for doing this is that I would like to make Storyboard to look as close to the real thing as possible. Right now if I provide a data source, the static cells in the storyboard would have no effect on the actual output and is not in any sense linked to the actual output.
Yes, it is possible. Set the custom class for the custom cell. If you wish to customise it from code, just connect it as an IBOutlet to the UITableViewController.

iOS : How do I slide a list item to expose commands (w/ storyboards)

I just started iOS development (I've been web dev for 6+ years) and I'm struggling with understanding how to build my UI beyond the default elements.
I feel like I started at kind of an awkward time. The resources I can find on creating a custom UI forgo the use of storyboards. Yet Xcode5 seems to force the use of storyboards (granted it's in beta so this may change).
While my current goal is to make list items slide over to expose custom actions (similar to mailbox, cobook, and countless other apps), really what I'm asking is for good resources on creating more custom UI's while still using storyboards that will lead me in the direction I need to go in order to do that.
Any help is appreciated, I've been desperately fighting the urge to take the easy way out and just use PhoneGap.
Thanks guys.
Check out this guide. In it they show how to make UITableViewCells with horizontal pan gestures so you can create the sort of effect that you are looking for.
http://www.raywenderlich.com/21842/how-to-make-a-gesture-driven-to-do-list-app-part-13
It uses a .xib file (I agree with you about the degree to which people using xib files in tutorials is incredibly annoying when storyboards seem to be the direction Apple is going in). However, you can make a UITableViewController in the storyboard. I would mostly use this in order to learn how to make custom UITableViewCells that can then be used in any sort of UITableView. You simply need to select the cell in the tableview, make the class of the cell the type of UITableViewCell you want (under the "Show Identity Inspector") and then make it the class of your custom UITableViewCell. Then when you call dequeueCellWithIdentifier, make sure the identifier is the same as the cell in the storyboard, and also cast the cell to your custom type like so...
MySlidingTableViewCell * cell = (MySlidingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
And make sure you #import the class for the cell.

Resources