How to work with dynamic content - ios

I am working with a UITable View and I am trying to figure out the best way to handle the way I display the content as its dynamic.
For instance some of the things I search may have a title and a description but then other times only have a title.
In a n instance like this I would like to be able to control the positioning of the labels in my UITableCell, of which I have created a custom one.
For instance this is a graphical view of what I am trying to do.
Title:
No Title:
As you can see with the second option I have decided to move the UILabel to the left where the Title label would be.
So What would be the best way of doing this?
I am thinking maybe creating several different format types in a .xib and then just using if statments.. if its missing such and such then use cell a or b or c.
Or is there a better way to achieve this?
Any help or advice would be greatly appreciated.

The different formats in .xib files will do the trick, just instance the proper cell type according to your needs, the other is delving into how you draw your cells, adding labels as you see fit when you get your data and drawing them appropriately.

Related

How i can use multiple nib files with multiple arrays?

I am creating my iOS app, and i during that , now i have to build scrollable screen where there is image view, labels, collection view, Reviews etc (Prototype Image is attached). I am wondering is it possible to make that type of screen with UITable view using multiple Nib files? (If it is possible, kindly guide me as how i should use multiple Nib files with multiple arrays). And , if it is not possible, how else i can go for it? Kindly guide me, and sorry if thats a duplicate because i have searched but i was not able to find any good answer.
There are several approaches that you can do.
If you want to use UItableview, then you need to create multiple
UITableviewCells, where each cells is based on the section of the design. Then you can load it by using array, as you logic stated above. For example indexpath[0] you'll load the cell with the picture(lets call this pictureCell), then indexpath[1] you'll load descriptionCell, and so on.
The other approach is using Scrollview, with several UIViews on a vertical stackview. And then you'll create a UIView Subclass based on the section of the design. This is usually my go to solution.
I'm not sure what "array" is refering to here, but by the look of your screenshot, I would go with a StackView inside a UIScrollView, or something like that. No need for a UITableView if you don't have a repeting pattern, you can just stack multiple views in a scroll view.
I hope it's clear enough.

Best way to display tabular data like the attached screenshot in Swift?

I am looking at the lower portion of this screenshot. If I had something similar to this in my design, what would be the best way to set this up in Swift?
I have looked into UITableView, but that doesn't seem to be quite what I need. Most of the tutorials & articles I've read regarding the UITableView only focus on 1 or 2 columns max, and don't really expand on how to go much beyond that.
I briefly considered just doing a bunch of statically placed labels, but realized that I will not always know exactly how many items are going to be in the table, so I need a more dynamic way of going about it.
Does anybody else have any advice on how I might go about achieving this sort of layout in Swift?
There are several ways...
UITableViewCell subsclass. It's perfect for your purposue, and you can design it's contents following your needs. (No matter if 2, 3 or 4 columns)
Combination of StackView Vertical with StackView Horizontal inside. Each horizaontal view will contains one of your row.
Generate a HTML page and injec it in a SFSafariViewController or WebView.
Static content. A plenty of UILabel. One for each piece of information
In my opinion the best aproach is the UITableViewCell subsclass.

Best practice add multiple object at one page on storyboard

I am wondering what is the best practice for creating a page in storyboard that consist of multiple object (It can be imageView, View, label, etc). Look at this image, I feel ridiculous adding new object under the bottom view that is outside the page view.
I know a little how to arrange view with relativeLayout etc in Android. But how can I arrange this page with storyboard and autolayout? I want to arrange a page that consist of multiple object with different size. This maybe a stupid question, but I mean it a lot for me as a newbie developer. Thank you.
Its hard to say without knowing more about what you are trying to do.
It looks to me like it might be a good candidate for using a UITableView. You'd create a table view and add cells for each of the items that you want.
You could also create a scroll view and install the items on the scroll view, either through custom code, or with AutoLayout if there are a fixed number of items.
You could also use a UICollectionView if you need a layout other than a vertical scrolling list of items.
Edit your question with more details on what you're trying to do if you want more specific help.

Creating NSArray of UITextField objects

I want to have a large number of text boxes which will be touch enabled and editable. Is creating NSArray of UItextField objects the best way for this? If Yes, How can I create? or Suggest other ways to achieve this.
It largely depends on what you are trying to do. An NSArray as a way to store all the text boxes you are using in you controller (instead of creating ivars for that purpose) is ok, but you could as well use a UITableView/UITableViewController for that.
Using a table view would give allow you to grow the number of your text boxes without any effort. On the other hand, if you can guarantee that your text boxes will never be more than those you can display on a single screen real estate, I don't think using a table view would give you big advantages. But, as I said, this largely depends on what you are trying to do.
If you decide to go for the array option, I would suggest using an NSDictionary instead, so that you can access each one of your views by name (or tag, if you associate a tag with each one).
Also keep in mind that you could use the getViewByTag: method on your container view to get a reference to any view that it contains based on the view tag you assigned. So, you could do:
//-- creating text box:
UITextField* textBox = ....;
textBox.tag = 1;
[self.view addSbview:textBox];
//-- accessing the text box:
UITextField* textBox = [self.view getViewByTag:1];
In this sense, a view already behaves as a container for you text boxes and gives you access to them.
EDIT:
Actually I'm trying to create a crossword grid
ok, so, if it's 2-dimensional, I would say that a table view is ruled out (it is not impossible to do, but I think there are easier ways).
as to your question, it all depends on how dynamic your crossword grid is: does it always have the same number of rows and columns? or can it be defined by the user? etc.
In the first case, I would go for an NSArray, or I would simply use tagging as shown above (that would also make memory management automatic).
Otherwise, you might inspect UICollectionView.
If your question is: which data structure is more appropriate to handle a crossword puzzle? then, have a look at this post. In any case, I would say: do not expect that you find a ready-made solution for that kind of problems...
A UITableView containing editable cells would be the best way to do this, if you're after what I think you're describing. There's lots of sample code on Apple's developer site detailing how best to use a table view to create a view showing a series of editable text inputs.
Better to use UI Table View instead of adding 'n' number of text fields.

iphone/ipad how to handle lots of input fields?

I'm writing a app that contains quite a bit of input fields for collecting data.
and im wondering what are some good methods to display these kind of input fields, if there are too many to fit on a screen? so something like the add contact screen... where u can scroll down and there are fields there
my initial idea is to put them in a scroll view and then i can scroll through them, is there a tutorial to do this? it seems like the scroll view is more for dynamically displaying data like a text view, and not for static forms like i was describing.
if anyone has any different methods for doing this please post.
UITableview will match perfectly for what you need.
My friend wrote this which is a container view that automatically helps with moving fields out of the way of the keyboard - It will certainly save you some time if you don't want to use a UITableView:
https://github.com/mackross/GTKeyboardHelper
The other way as H2CO3 suggested is to use a UITableView. If it is a UITableViewController, then you get the moving out of the keyboards way automatically. You could build custom table view cells that are styled to have a prompt and a UITextField for input. You can also set the selectionStyle to UITableViewCellSelectionStyleNone to prevent these cells from highlighting when selected.
I think the third way is to do this with a UINavigationController (or a UIPageControl) and build a kind of wizard, where you go through various pages of related data. This might be the neatest way depending on how many fields you have and if you can group data into common sets (e.g. personal information, work information etc)
I had the same problem and found GTKeyboardHelper to be an easy way out.
After drag and drop the framework in your project, include the header file.
Download and open the example project, then drag the "Keyboard Helper" object from the objects section in the xib to the objects section in your project's interface builder.
Drag and drop all your views to be children of the "Keyboard Helper".

Resources