Creating tableview cell content programmatically - swift

I want to create UItableViewCell ui programmatically based on API response.
I'm using a API that output a set of articles, but the content of these articles could be deferent, for example some articles may not have a description, or some of them may not have a image. I want to create Tableview cells programmatically based on these data.
I have tried setting constraints in viewDidLoad method of the cell but It does not work.
Do you have any suggestions on how to do this?

Depending on how different the articles are from each other, you could either choose to design multiple different kinds of cells for your table view and choose which cell to use for a given row based on the data you have, or you could design a single cell that uses a stack view to hide the labels that you don't have data for. When you set a view inside a stack view to isHidden = true then the stack view will resize its subviews as if the hidden view does not exist.

Related

TableView with different cell types or ScrollView with inner TableViews

I saw many stackoverflow questions, but I could not come to a definite answer.
I need to create the following screen (detailview):
It's wide ScrollView screen which contains among other custom UITableViewCells of different types.
NewsCell and SongCell - are two custom UITableViewCells classes, which appear differently.
How to properly implement such a screen?
Option 1:
Use UITableViewController and UITableVIewCells of different types:PhotoCell, DescriptionCell, NewsCell, SomeTextCell, SongCell.
Option 2:
Use UIScrollViewController, which contains other elements (Image, Labels, UTableView for news and UITableView for songs. (But I read that insert UITableView into UIScrollView it's wrong, because UITableView is subclass of UIScrollView)
Option 3:
Something else.
Waiting for your comments!
Create multiple custom cells and start integrate into your tableview. If you want to show the cells based upon the content you can calculate the internal elements size and take the highest element hight and make the cell height. Now you can observe that cells will display based upon the highest element hight.I think it will full fill your requirement.
If there is a pattern like a header followed by few cells, then you can use sections which is a feature in uitableview.

Newbie TableView challenges

I am building an app that requires a tableview control so that users can select multiple rows and act upon them in some way. I find it easy to load my data and display it using the UITableViewController but it seems when I do it this way I am unable to place any other controls on the page, such as a toolbar to give the user some actions to perform on the selected rows. I can place a toolbar control on the form in the storyboard, but it doesn't render in the emulator.
Using a UIViewController and placing a TableView on it seems to come with its own set of confusing challenges (that will make total sense once I conquer them).
Is there any advice for a smooth way of getting a table view with toolbar controls? Thanks!
don't use a TableViewController. Use a Standard ViewController, then add a UITableView to it, and adjust the size. This way you will be able to do whatever else you want on that view without limiting yourself to the tableView only functionality.
When you do this make sure you add the datasource and delegate to the connected table. Then add cellForRowAtIndex, number of sections, number of rows, and whatever other delegate methods you need for your table.
Good luck
Is there any advice for a smooth way of getting a table view with toolbar controls?
Yes there are few ways to accomplish this, one way would be adding a footerview to your tableviewcontroller check these
http://developer.apple.com/
UIButtons on tableFooterView not responding to events
Easier way to add this is using storyboard.
Just drag and drop a view in to your tableviewcontroller, then you can adjust the views size and put anything from objects menu to inside of the view.
Now the problem with that is view will not be stable position at the bottom of the page like a tabbar. Lets say you have only 1 row in your tableview, footer view will go up just below that 1 row, lets say you have hundreds of items in your tableview toolbar will be at the bottom of the rows.
The other solutions for your problem would be either create a custom view and adding it to current view or window (this is little bit adbance),but if you want this just google it.
Or just as you said, create a viewcontroller and put a uitableview inside. Dont forget to add <UITableViewDelegate,UITableViewDataSource> to your .h file and then you can call UItableview delegate methods.\
Good Luck.

how to use UITableViews for multiple datasets

I am currently playing around with a navigational based app. Where I want to allow the user to construct a search query by selecting one of several different uitableview cells when touched leads them to a sub uitableview where I will display the data for the user to select.
each cell will load the same subview however it will load it with different datasets. I am wanting to know an appropriate way of handling the transition of data (when the user selects the cell from the subveiw how can I control which cell that data should be sent back to?
I am thinking about passing the subview the Indexpath of the mainviews selected cell.. then passing it back to when the subview is poped from the stack so that it knows where the data needs to be.. is that the best solution? or is their another way of doing this?
Yes, give the subview a property for the indexPath of main view's selected cell. Set this property in the main view's didSelectCell method before pushing the subview.
Once you pass that indexPath back to the main view with you data, you can use
[self cellForRowAtIndexPath:indexPath]
to access the correct cell.

iPhone -- customizing a grouped table view in Interface Builder

I am writing an iPhone app. According to the design, it is supposed to contain a lot of grouped table views. In these views, the rows are frequently not similar to each other. For example, on one screen, one row is the name of a task, another is its owner, another is its description, yet another is its history (which is supposed to be an expanding box), and so on. These rows are edited in different ways. For example, the name can be entered free-form, but the owner has to be chosen from a list, which would be brought up in a further table view.
I think doing all of this programatically would drive me batty. What I want is a way to design these screens in IB. But I can't figure out how to get IB to do treat the cells individually. Is that possible?
Thanks.
In Interface Builder you create custom UITableViewCells for each row and then return the appropriate custom cell in – tableView:willDisplayCell:forRowAtIndexPath:. You also need to return the height of each custom cell in – tableView:heightForRowAtIndexPath:.
Within Interface Builder, you create a custom cell just like you would any view. You size the cell and then fill it with subviews. It's best if you create a UITableViewCells subclass for each custom cell that has IBOutlets that connect to each subview. That way you can easily access labels, imageviews, controls etc.

Multiple images per row in UITableView's cell

Is there any sample code that would illustrate how to have multiple images within each row?
Typical apps show a thumbnail to the left side with text to the right. I'd like to do that plus an image to the right of the text.
How would I go about doing this?
In interface builder, simply create a tableview cell that looks like you want. Then create a UITableViewCell subclass that has properties pointing to the elements of the new cell. Set the class of cell to the subclass then add cells of that class to the table in the standard way.
A tableview cell is just a view and you modify it and use it just like any other view.
You'll have to create a custom UITableView cell. Here's an example of using multiple UILabels in one. Here's another.
Pretty easy - follow Apple's documentation to create exactly the cell you want in Interface Builder with as many UIImage or whatever else you like. Look at Table View Programming Guide for details on how to make and load the custom cells - just be careful about performance when you put a lot of visual elements in a table view.