Two different UITableViewCells with same xib [duplicate] - ios

This question already has an answer here:
One xib, several sub classes
(1 answer)
Closed 4 years ago.
I have two UITableViewCells that looks exactly the same, but each one has a completely different logic.
So I want to create two UITableViewCell, with the same xib.
The logic of each cell is different and I don't want to have code split with ifs.
How do I do that?

If its a same UI than you shouldn't create two UITableViewCell.
What you can do it write two different updateView method in a same uitableview class.
This way you can reuse the code and also if there is design change you just need to change in one place

Related

Should I reuse tableView cells when they have little difference? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I just want to know which is the better way to do cell reuse
For example:
Cell 1:
Name
Detail
Time
Cell 2:
Name
Detail
Image
Time
The two cells, "Name","Detail","Time" are having the same position.
So, my questions are:
Shall I use one cell for reuse(modify constraint in runtime) or create two separate cells?
I think use two cell will have the better performance, but how to reuse the "Name","Detail","Time"'s auto layout in xib (If use code, this is easy for me, but my project use xib)
You have many options.
use stack view, then manipulating constraints is not needed to hide a element. Lets you only create one storyboard/IB instance and stack view handles hiding nicely.
use two unique cells, they can grow over time down separate paths as requirements change.
Or my preference
create cell subclass, lets say BaseCell, that loads object or
protocol that can supply name/detail/time, cell.load(xxx)
create a cell that subclasses BaseCell, say ImageBaseCell, that loads
an object that loads object or protocol that can supply
name/detail/time/image, handle image here, but super.load(xxx) will
handle the rest.
This does require individual xibs per cell subclass. However, this way, logic for handling name/detail/time is in one place, instead of several cell classes.
I would recommend for going with two separate cells. The table view datasource methods will take care of populating the cell contents rather than you handling the views inside cellForRowAtIndexPath method. Also if you're incorporating contents of varying sizes in the a cell, it is always better to go with multiple cells for cells having different contents.

What is the best way to design a form layout [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
What is the best way to design a form layout, I've tried using static UITableView, but I don't know if it's a good practice when I have many cells and if I use prototype cells, it's difficult for me to interact with the elements inside the cells.
Other form is without using UITableView only a UIScrollView, but I don't know which is the best form.
In short, which is the most used method for design a form layout?
EDIT: I need to achieve something as shown in the image.
EDIT2: In the case of a CustomCell have a picker view inside, it's better that the same cell delegate from the UIPickerView or it should be the viewcontroller that delegates from the pickerview?
Now my CustomCell delegates from a UIPickerView and the options strings array and protocol methods are inside that class, that's okay?, In that case, what can I do when the array is composed of custom objects instead of strings, for example, I have an array of Persons and I need to show in the pickerview the person name, but I need to return to the viewcontroller the person's age. In that case, I have to create other custom cell that manage that array type (and another different one whenever I need it to handle a different type array)?, or is there any way to unify that to avoid having to create many of different cell classes?.
The best method herein would still be to use UITableView. You can design your own custom cells which gives you better flexibility incase the form changes in the future.
If the cells have similar kind of elements and you want to reduce your workload for the auto layout stuff you can surely go for the UIStackView inside the custom cell you are designing as suggested by Donovan.
The main reason for the usage of the UITableView is that it gives you great flexibility in terms of grabbing the indexPath for a particular row. Even if the form increases in terms of the numbers of rows it needs to hold on to, you do not need to add a separate UIScrollView to the same.
If the design for certain rows changes, while other still being of the same design, you can still use custom cell and put in a different cell identifier to the new cells and use the "dequeueReusableCellWithIdentifier" and incorporate the new design.
Hope this helps!
Table views are generally used when you're displaying data to the user of some sort. For your case how about trying a UIStackView. Here is a guide to get you started.

How to have different elements in collection view? [duplicate]

This question already has answers here:
Displaying two different cells in a collection view - Swift 2.0 iOS
(2 answers)
Closed 5 years ago.
so I try to have collection view but been thinking is it possible to have different element in each cell view? For Example: in the first cell I have the whole cell covered by UIImageView while some specific cells only got UITextView.
Here's an example that I'm talking about, Medium iOS App.
Create several prototype cells, each formatted with whichever UI elements you want, and dequeue the appropriate cell for each different type of data you want to display.

Making a single Prototype UITableCell for use with multiple controllers [duplicate]

This question already has answers here:
In a storyboard, how do I make a custom cell for use with multiple controllers?
(7 answers)
Closed 6 years ago.
This post explains why a single Storyboard prototype UITableCell cannot be used for multiple Views:
In a storyboard, how do I make a custom cell for use with multiple controllers?
However, that was for Objective-C in 2012. Is it still true now for Swift, and if not how is it achieved please? Thank you.
Yes it is still true. It's in a limitation of the application frameworks, not of the language. That said, it is possible to register a nib file that defines a cell and share it across multiple view controllers

Can I use more than one custom tableviewcell nib file in a UITableView?

I want to put different cells in a table, and all of them is kind of complexity, so I want to use nib for these cells. However, i really don't know how to use multiple cells in a table. Can you help me?
You can use as many custom UITableViewCells as you want in a single UITableView. The issue here is: What kind of logic do you want to implement? Only you can define it. You can start by using this tutorial to see how to implement the custom UITableViewCells:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
You can then reply to my answer to tell me what logic do you to implement. For example:
first 3 cells one with a type of UITableViewCell, then 3 with different UITableViewCell.
Alternate between cells (for instance, 4 different UITableViewCells in a row)
You need to specify what you need. :)

Resources