Dynamic Custom Cell - iPhone - uitableview

I have created a custom UITableviewcell using IB which holds for 1 user. If i need to create the same cell dynamically based on the number of users, what should be done. Could any one please help me with this.
If the data comes for a single user from XML i need to show a single cell with data for 1 user. If the data comes for multiple users, I need to show multiple cells with data. Any help is Appreciated.
Thanks

Get array of all users and send the send the count information in the numberOfRowsInSection UITableViewDataSource method and populate the data based on the array in cellForRowAtIndexPath method.

Related

What is the best way to get the values of each TableView Cell to send back to the server?

I am writing a new app in swift and I'm new to swift was well.
I have setup each of the cells in the tableView so they ask for a specific type of answer. Things like multiple choice, Yes/No, Quantity and Photo. I have the answers in the cells as I can log them out when we make changes to things like the yes/no questions. But my problem is I need to get each question, its type and the response the user gives to serialize in JSON to send back What is the best way for me to get this data so I can send it?
Thanks in advance for everyones help
Just a short overview, So you get your answer
UITableView is highly optimized, and thus only keep On-screen visible rows in memory. Now, All rows Cells are cached in Pool and are reused and not regenerated. Whenever, user scrolls the UITableView, it adds the just-hidden rows in Pool and reuses them for next to be visible rows.
So, now, coming to your answer
UITableViewCells won't save user selected values
Best solution will be to pro-actively save user selected response in NSMutableDictionary for each index.
For eg., if user taps on yes, then save that in datasource on tap event and keep updating your cell view in
tableView cellForRowAtIndexpath
And send the same saved data to server.

iOS Swift How to Extract Strings from All Selected Rows in a TableView

I want to loop through a TableView and extract the text from all the selected rows. I suppose I "could" create and maintain a special array that is updated every time a row is selected/deselected using the didSelect/didDeselectRowAtIndexPath methods. But creating a separate array seems like an extra step. Is there no way to let the TableView itself serve as the array and then simply loop through it and get the selected rows? What would the code look like? I'm new to Swift, so this might be a silly question.
Part of the problem is that cells are supposed to be reused, and when used this way it is not possible to loop through them all. You could get around this by using a unique reuse identifier for each cell, such as the indexPath itself or some underlying unique id in your model. Then, you could indeed loop through all cells and retrieve whatever state you desired from each.
You would, however, find your application crushed under the weight of too many cells being instantiated and kept in memory. If you don't have many cells you won't be killed, but try it with a big data set and your app will enjoy a very quick death.
It is far more efficient to store one array with a bunch of id's than a large number of memory-intensive UITableViewCells.
As mentioned in comments, you should work with underlying datasource, not the table itself.
For example if your table shows rows from Array, it is way more faster to retrieve strings directly from that array than creating UITableViewCells and get strings from them.
Get indices of selected rows using UITableView's property indexPathsForSelectedRows.
Query datasource for each row.
As has been said the tableview only handles displaying, your datasource is what powers the data shown if you think about it.
Plus as said before the tableview dequeues cells as they scroll on and off the screen.
The best way to achieve what you want is to add a property to your datasource for each element that will allow you to filter out the select properties easily.
How are you storing the state for each selected cell currently? As this is the same functionally you would use to be able to generate your selected text array.

How to read data/test from each row of a UITableView?

In my iOS app the user is supposed to fill in all fields in a table where each row may contain a:
a text view, or
a date picker or
a picker view, or
a switch.
I have created 7 different types of cell each of which contains one of those UI components with its specific tag. In this way I can access them when the table is only in "read mode".
The last row of the cell contains a "Save" button. When pressed, all these values should be collected together into an object which will be stored in a database.
The problem is that I don't know how to read those things from each row.
Is there a specific method?
To get all data from your cells best way would be to have an NSArray that will have all the data you need, and update these values in NSArray for corresponding data in the cells. You have to use some kind of dataSource to show the data in the tableView and you can use that exact NSArray to update values and on save to simply go through it and save these values.
For the other questions:
#NickCatib 1) I don't understand what the method func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) does. Is it used to get data from each cell? When is it called? (in other words how can I link it to the IBAction of the submit button)
That method is called when you select particular cell. You can link it with selectRowAtIndexPath:animated:scrollPosition: method.
#NickCatib: 2) is it better to assign to each cell an unique tag to read the data when the submit button is touched?
You can, but again, better way would be to iterate thourgh NSArray as mentioned above.

Add data to UITableView when scrolling

I need to show a list of elements from an array in a UITableView (loaded from database).
The steps are:
When I open the view I fetch the data from the database and I store it in the array. If the array has less than 100 objects I perform a call to the backend to get 100 objects.
I show the first 10 objects in the table view while the backend call is performed. If there are less than 10 objects I show them and when the call to the backend is finished, then I show the needed n-objects in order to have 10 in the table.
Now, each time I scroll to the bottom of the table I need to add the next 10 objects from the array, and when the table has 100 objects - don't show more (this is the limit).
Please not that there are might be many possible solutions to this. Some might be better than this.
Create an extra cell with a reuseIdentifier like LoadMoreCell, which should appear at the end of your table.
Implement UITableViewDelegate method tableView:willDisplayCell:forRowAtIndexPath:.
In this check if the reuseIdentifier is LoadMoreCell.
If it is, call your method that requests data.

iOS 7: Two different heights for cells in a table inheriting from the same UITableViewCell

I need "Two different heights for cells in a table inheriting from the same UITableViewCell".
A bit more on this. I am using iOS 7 and storyboard. I created on the story board two different UITableViewCell prototype for a UITableView with custom cells.
I then created a class, MyUITableViewCell which defines the beheaviour of the cell as well as a protocol method that is then implemented by the delegate (which is in my case is the UITableViewController class where the UITableView containing the cells is).
Now.. I would like to dynamically set the row of the cells according to whether the cells is of type 1 or type 2.
I have found this method:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// if(indexPath) refers to cell of type 1 then
// return 240;
// else return 120;
// I created a NSDictionary with data that includes the type of cell information, however I am not sure how to link indexPath to the dictionary. The keys of the dictionary are ids and not array indexes.. so I am a bit lost in here..
}
As said in the comment:
"I created a NSDictionary with data that includes the type of cell information, however I am not sure how to link indexPath to the dictionary. The keys of the dictionary are ids and not array indexes.. so I am a bit lost in here.."
I would like to find a solution to this but not sure if it is possible using only NSDictionary or if I need some other hack...
Sure you can do that. The cells in your table view are data backed, you must have access to the data that defines the table in the table view delegate.
If your table is simple with only one section then you might only need to look at indexPath.row. There must be some way to relate this value to your data, if there is not you will not be able to populate the cell in cellForRowAtIndexPath either.
Often the indexPath.row value can be used as an index into an array containing data, such as [tableDataArray objectAtIndex:indexPath.row]; In your case, if the data is a dictionary, what are the keys? Maybe you can make a key directly from the row ([NSNumber numberWithInteger:indexPath.row], since a key must be an object) or maybe you need to use an array to translate the NSNumber produced from the index paths into the keys that are used in your dictionary. If your data is pre-existing and it would be a lot of work to change it this might be the best way, otherwise think about organising your data for easy access.
An alternative would be to use a UICollectionView, where with a custom UICollectionViewLayout you can define the frame of every cell individually at the time you report attributes for the cell. However you still need a way to relate index path to the underlying data. It is more versatile than a UITableView and is possibly a more useful skill to develop with the state of iOS development today.

Resources