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

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.

Related

Is it good practice to ensure cellForRowAtIndexPath doesn't contain cell view modification code? [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
I came upon this thought recently when I was working to add a 'dark/night' mode to my app.
Regularly in cellForRowAtIndexPath I would of course 'connect' my datasource to my newly created/reused cell.
Setting the cells data/strings/values etc, ready for display.
However, whilst cognisant of MVC, I myself, would sometimes set cell background colours or some other cell subview view related property (animations/colours/text style). - I'm working if this is truly a bad coding style?
During my work to create night mode, I moved ALL code related to view modification (colour/style/aesthetic) into each view class.
Then upon a reload of my tableView, I setup up each cell and finally call their new function to layout their aesthetics - which now lives inside the cell class.
Ergo, my cellForRowAtIndexPath has a single call to the cell instance to change its look and feel, and the rest of the code is purely setting data on the cell.
Is this the pure MVC approach?
Is this good practice?
Is it acceptable to be calling my tableView's reload method in order to trigger a call to update the cell as needed, whenever my viewController wants to change look and feel, or should I be keeping a reference to each cell separately?
The answer to your question is pretty subjective.
On my opinion in most cases the outlets of cell should be private to the cell's class, and not expose unneeded info to the viewController. I prefer creating several methods within the cell's class such as: func populate(_ with: SomeDataType) - for populating the cell. This way the cell is much more encapsulated and reusable.
Is it acceptable to be calling my tableView's reload method in order
to trigger a call to update the cell as needed, whenever my
viewController wants to change look and feel, or should I be keeping a
reference to each cell separately
Yes it is acceptable, in fact there are pretty rare cases that you need to keep references to reusable elements, such as UITableViewCell's instances. Please take a look at the section Reloading the Table View from the Apple documentation or func reloadRows(at: [IndexPath], with: UITableViewRowAnimation) link
EDIT 1:
I feel like you can benefit from going trough this tutorial: https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html
The main goal is: Try to understand what is "reuseIdentifier" and why UITableView reuses cells instead of creating new views every time that cell should be displayed. Once you get this, you will understand why saving reference to tableViewCell is tricky.

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 customize a UITableview's index look? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
http://imgur.com/rxVYsV2 - reddit's index
I want to create an index similar to the one in reddit's app, especially adding icons to it and having it highlight the letter(see link for picture).
Any suggestions for how to do it(in xamarin ios)?
Since, you didn't ask for code, I'll just help you get started. :)
Use a UITableView to create a row, a cell would be like
Add another UITableView to create the side listing, with just an alphabet as cell. Something like
Now, on selection of any row from the second UITableView, modify the highlight and scroll the left UITableView accordingly. As well as on scroll of left TableView, change the highlight according to the first alphabet.
UPDATE
For StoryBoards follow the following steps:
Drag and drop two UITableViews in StoryBoard, place and resize them however you want (according to the layout you want), apply constraints.
Give each StoryBoard a reference like you do for other elements such as buttons etc. or give them a Tag.
In your ViewController make two Nested Classes, two of them for 1st TableView, one inheriting from UITableViewDataSource and other from UITableViewDelegate. Override the mandatory methods (link).
Now once you have the classes done, assign them to your TableViews like
//With Tag
UITableView myTableView1 = (UITableView) this.View.ViewWithTag(1);
myTableView1.DataSource = new MyTableView1DataSourceClass();
//OR
//With reference
myTableView1.DataSource = new MyTableView1DataSourceClass();
myTableView1.Delegate = new MyTableView1DelegateClass();
Please look at Create UI Objects for more help with making objects.
Please upvote and mark it as the correct answer if you feel it is. Besides, let me know if you need more help. Have a great time coding :)

UITableView for a list of Controls [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 7 years ago.
Improve this question
I am trying to understand when one should/should not use a UITableView.
I do know, if there is a table of information UITableView would be the perfect fit. But what if there are rows of "controls" ?
See the image below.
What are some of the advantages and disadvantages of using a UITableView to achieve layout above?
What alternatives to a UITableView exist for achieving such a layout?
This comes down to the question of how many controls/rows you will have and what they represent.
Dynamic Number of Controls Based on Model
A tableview is the perfect solution. The complications with having controls in a table view cell that is dynamically generated:
1) You cannot add IBActions for controls in table view cells. You will need to add a target/action to each control when the cell is generated.
2) Your action method will need a way to determine which element in your model the cell is displaying when selected (since cells are reused, controls will correspond to many model objects). To do this you need the index path in your model corresponding to the selected control. This can be done as follows (assuming a button but should work for any control):
Determine the control's position in the tableview:
let buttonPosition = button.convertPoint(.zero, toView: tableView)
Find the index path
let indexPath = self.indexPathForRowAtPoint(buttonPosition)
Then query your model for the object at the index path and respond accordingly.
Finite Number of Controls
The above is needlessly complex if you have a finite and known number of controls at runtime. In that case the above layout can be easily achieved with a UIStackView.
See StackView Documentation
Having controls in tableViews is perfectly fine.
You may want to know that tableviews have scrollable content and selectable cells, so your interactions will be slightly different than if you directly put your controls in a view.
Row selection and bouncing bounds can be annoying, as well as the delayed tap while touching inside a scroll view.
But if you need your controls to be dynamically shown depending on your settings, it can be done with the tableview data source or delegate methods.

When should I use a data source (protocol) vs setting the classes data on a property or on init? [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 9 years ago.
Improve this question
I've seen a data source on a ton of Apple's code; UITableView, UICollectionView, etc.
I am making a custom view which does not use the view reuse like my mentioned examples do. Should I just set the data in the init method/using a property, or should I make a data source protocol on my custom view and get the data right when I want it from the client objects? Or should I do some other thing I don't know about...
Thanks!
Well... it depends.
If your view is presenting a lot of information and optimization techniques are needed to perform smoothly, then a dataSource delegate pattern should be used. If it's just a handful of small bits of data that needs to be displayed, just use properties/init.
UITableView and UICollectionView are heavily optimized views that do a lot to keep smooth scrolling performance. They can also present a large amount of data, which might not be practical to have all loaded in memory.
On the other hand, something like UILabel doesn't usually present a lot of information, so it uses properties to configure the data it needs to display.
When creating a custom view, usually parameters are set in -initWithFrame:(CGRect)frame method, that is called for a UIView only once.
But what if somewhere later you'll need to update smith of them?
In that case i'd make an updateView method, that can be called from another classes and make any changes.
All values that can be changed make a local variables in order to access them in that method.

Resources