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

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.

Related

Getting the data of all TableView Cell which all has TextFields in it (including the hidden views)

I'd like to get every data that is within all cells in one tableview which is quite a long list.
I'm looking for an approach on how to retrieve everything including those hidden in view, which I know the views are reused. I think some of you might have experienced this problem before, what are your approach on this?
I've tried
let cells = self.tableView.visibleCells
then looping into every cell and saving each data to an array but it is not effective in getting those that aren't part of the view or hidden. Is there a way to get over this?
In cellForRowAtIndexPath, YOU are telling the table what is in each cell. So why would you turn around and ask the table what's in each cell? If the user puts "Hello" in your first cell, then scrolls the table enough to push that first cell out of view, then when the user scrolls back to the top, YOU are the one telling it to put "Hello" back in that first cell. YOU own the data source, not the table.
You need a data source. That can be "empty" at first, maybe an array of empty strings if that's what you want (each index in the array could map to a table row for example). But then, as the user interacts with the text fields in the cells, you need to update that data source with the text they entered.
You should use that data source as your source for the cellForRowAtIndex method. That way you can handle populating the cells when they are requested by the table, and you also know all the data when the user is done.
Why not just update the model each time the user taps a key when editing a textfield? You could create a protocol for that cell subclass and make your view controller the delegate for each cell. As long as cells are guaranteed to stay on the screen while you're typing (you'll get some weird behaviors if not) the cell can send a message to the view controller or whatever you hook it up to telling it what new value to store. Then everything is already stored for you when you need the full list, and you don't have to interact with the tableview.

Select not visible cell inside a collection

Well, one might ask why should you do that ?
I have a collection view inside some view where the user can select a cell.
Then this selection is saved and later, when the user wants to enter that view again, he should be able to see his previous saved selection.
If his selection was at the end of the collection view, I will not be able to load the collection view with his previous selection, because:
for cell in collectionView.visibleCells()
// find and select previous cell
Will not loop through all cells, specifically the one the user chose (that does not exist yet).
Is there a solution to this problem?
You can remember the exact index paths of selected rows (using indexPathsForSelectedRows), but this is still not safe if your dataSource set changes/updates and the remembered indexes may become invalid with new/different set of data. Thus you should remember selection of “real world” objects (where you're filling specific cell data, like title etc.) for example by remembering object identifiers of your choice so you'll be able to re-map and re-select rows in case of reload with different data. The management of objects selection is generally completely in your hands.
Well after a lot of searching, what works for me is to set a local variable say: selectedCell , then when I want to show a previous user selected cell, i will just set the variable , and on :
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
if(indexPath.row==selectedCell)
}
I am checking if its already selected and mark it .
I could not find any other clever way of doing so automatically .

Manipulating data through table view cells iOS swift

firstly thank you in advance for any help and secondly I am very new to do the iOS development and swift so please bear with me and forgive me if I ask any stupid question.
MY ISSUE:
I created a table view and created an array of numbers. I iterated through the array and displayed each number in the array in a different table view cell. Now what I want to do is either have a button my table view cell or a check mark. And when ever I tap the button or the check mark the number that is being displayed on the table cell from the array is selected and then I tap another button or a check mark on a different cell and that number also gets selected and when I click the "done" button the both numbers are added and displayed on my root view.
Its kind of like a order taking app you can think of each cell as displaying the price of a food item and you can selected multiple food items and once you click done it adds up the price and displays it.
Im not looking for any advanced techniques, anything that can help me do this will be much appreciated. Again sorry for any stupid questions and thank you.
This is how you should break it down:
1) Implement the didSelectRowAtIndexPath delegate method after the table has been populated:
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
2) Within that method set the checkmark for the row:
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
3) Declare a class Array:
var numberArray: [Int]
4) Finally, implement an array extension for the addition. Call it when tapping the done button:
How can we create a generic Array Extension that sums Number types in Swift?
There's some fine tuning depending on your implementation but I hope this gives you a head start.
Key things to look at:
UITableView has a var called allowsMultipleSelection. You want to set that to true (it's false by default.)
If you want to change the way a cell looks when it is selected, then you can either look into the table view's delegate didSelectRowAtIndexPath and didDeselectRowAtIndexPath. Or if you have your own subclass of UITableViewCell, you can override the setSelected:animated: method.
The basic idea is to give the user a button to say when (s)he is done selecting cells, then look at the tableView's indexPathsForSelectedRows to find out which items were selected.

iOS: Adding row to tableview

I have a tableview that is based on a array of DB results, these results contains a date field. I have a custom cell that contains an image and labels. I'm trying to do:
At cellForRowAtIndexPath I verify if the date of current item (objectAtIndex:indexPath.row) has date field bigger than the last item (objectAtIndex:indexPath.row-1). If this is true: I want to add a cell filling the ImageView with a certain image, but I need to add a new row just for show this image.
How can I do this? I'm already doing this verification, but I need to add a new cell...
Do not use the cellForRowAtIndexPath to decide how many cells you want to have. At the point this method is called you should have already setup the data source to provide table view with all information needed.
Here is what you need to do. Refactor your code in a way so you:
Setup the data source first.
Force reload of the table view either by calling the reloadData method.
hey you can add the object in your data base(for example ns array) and refresh the table view with method
[tableView reloadData];
then the method cell for row at index path will be called again and it will refresh the table view's items.just make sure the method cellforrawantindexpath in your code knows to handle the new data type(make validations).
Your tableView data source should not contain any of that logic where the content of once cell depends on the content of another cell. Instead, you should have a data item for each requested indexPath and that data item should contain ALL logic necessary for the cell to be configured. If an action on that cell has an effect on how another cell should look, you apply a change to the corresponding data-item, and then call reloadRowsAtIndexPaths: for the indexPaths.
In short: configure cells ONLY in tableView:cellForRowAtIndexPath: or tableView:willDisplayCellAtIndexPath, and ONLY do configuring. Other logic should be placed in some data(-controller) object.
As suggested, you should add an item to your data-array. Then call -insertRowAtIndexPath: on the tableView. ReloadData is like a big ugly hammer that you only use when ALL of the data for that tableView changes.

Dynamic Custom Cell - iPhone

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.

Resources