Getting data from a custom cell - uitableview

I am dealing with a problem when I try to get my data from custom cell which has two different textfields.
The custom cell was implemented this way:
http://imageshack.us/photo/my-images/441/screenshot20120726at434.png/
And the table view looks like this:
http://imageshack.us/photo/my-images/849/screenshot20120726at431.png/
What I want to do is that when I press SAVE button from the right top corner I should get the values from the two textfields and also the switch status. I don't know how to manage the cells in order to do that. Thank you in advance!

You can create three index paths with section 0 and rows 0, 1, and 2. Then ask your table view for cellForRowAtIndexPath:. You know which type of information you want from each -- the same as what you stored in your datasource method -- and can read it from the matching text field or switch.

Related

How to use collapse option to display list of users

How to design and code this in swift(IOS) AND Xcode.If we tap on down button it should display items and vice-versa like this:
You need a Collapsable TableView. In order to achieve that, in your TableView you must keep track of which sections are collapsed (contracted) and which of them are expanded. For this you need to maintain a set of indices of sections that are expanded, or a boolean array where the value of each index indicates if the corresponding section is expanded or not. Check for the values at the specific index while assigning height to a certain row. Check this link for more help.
Refer ans by #Cristik.Refer my previous ans for more info.
You need expandable table view. Basically, you need to control the open and close of sections of your table view.
Here is an good example with working code & animation written in swift 3.0

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.

Using storyboard to create multiple TableViews that can be toggled between using a button

I'm trying to create an app where multiple lists can be switched between using a button. Does anyone have any advice on the best way to do this, preferably using storyboard (my Swift skills leave a lot to be desired)? Right now I only have one list (a TableView) which is contained in a Container View, along with two other regular Views, one at the top and one at the bottom of the Container View, with the TableView in the middle. I want to have a button in the top regular View that switches between multiple TableViews. I want the number of TableViews to be dynamic, starting with one, but then the user can add additional tables as they need. Any advice on how to set this up would be greatly appreciated! Is it even possible to create a prototype TableView? Is that the way to go here? I've found a couple of Answers on Stack Overflow regarding multiple tables on a single screen, but these lists wouldn't be on a single screen, they would only be viewable one at a time: if you want to see the second list, you have to press the "Next List" button (in the View at the top of the Container View), and the first list disappears and is replaced with the second. Thanks everybody!
Don't change table views, change datasources. Use a single table view as you have it, and one array for each mode that the table is in (call those arrayA and arrayB). Another array-type variable -- call it theModel -- should be set to point to A or B.
The datasource methods will answer the count of theModel, and get values for the cells from theModel. When the user presses the button...
self.theModel = (self.theModel== self.arrayA)? self.arrayB : self.arrayA
self.tableView.reloadData()
// everywhere else, use theModel as the datasource

How to distinguish 3 objects in a custom cells

I am making an iPhone app.
In this application I have to make a look like below.
I am not allowed to use collection view.
I am using tableview, and custom cells. Which I am easily able to incorporate. Means taking 3 subviews in Custom cells. And making a look.
Here the problem is In a cell, how do I distinguish each object. so that I can call each object, to set an image on image view.
Is there any Object oriented mechanism to distinguish all 3 objects in a cell ?
Try to get the data as NSArray of NSDictionary containing an array of 3 objects that you want to display on cell.
Assign tag to UImageView in the custom cell.
In cellForRowAtIndexPath, get the 3 objects and apply image using switch case.
Well everything depends on how you get the data from the server
You can use outlet collections which will give you an array of UIImageView and you can assign different tags to the imageview so you can assign to them.
You can use these guides to understand how outlet collections works:
http://nshipster.com/ibaction-iboutlet-iboutletcollection/
http://useyourloaf.com/blog/interface-builder-outlet-collections/
All the standard procedures should work to achieve this but then it only depends on how nice you want to do this.
The straight forward procedure is to expose the outlets of the image views and labels in the cell and assign the correct values to those when dequeuing/creating table view cell.
The first upgrade would be to rather expose 3 setters on the cell to simply set your model to each of them which will then internally set the images and texts inside the cell.
The next thing you may do is to rather insert an array of objects (always sending up to 3 in your case) instead of having 3 setters.
At this point you may actually rather use a collection view INSIDE the cell and make the cell a data source for the collection view. But this is totally optional.
Now since you may still dislike the table view data source you may create another model which contains an array of objects (again up to 3 in your case) and make a system that will distribute your original array of objects into the array of these containers.
If then you need to handle buttons or other touch events they may be handled with collection view delegate or 3 buttons and in both cases I advise you to handle those in the cell and create a custom delegate for the cell which will report the event with the appropriate model.
This together generates the following:
When you receive the data call a container class to distribute your array of objects (into groups of 3) and assign it to your table view data source (view controller usually)
Number of rows is the same as number of containers in the array
Cell for row assigns the container with row index to the cell. It assigns self as a delegate
Cell internally handles the object distribution either via collection view, separated outlets or outlet collections.
Cell handles actions and reports them back to the delegate (- (void)myCell:(MyCell *)cell selectedItem:(MyObject *)item;)
The cell delegate can again handle what to do upon reported actions
Also if you want to avoid a collection view inside the cell you can create a custom view using xib so you do not copy the labels, image views and such. Then simply create 3 of these custom views inside the cell. Also by using inspectable and designable these views will be visible inside the storyboard.
First I'll say that a restriction against using UICollectionView is silly. Are you still targeting ios5?
I'd look at it like this.
make your own view class for the 'subcell' let's use this term for any single instance of the 3 views per cell. I'd subclass UIImageView, adding the label for the name down the bottom and a 'setSelected:' kind of method to highlight when selected via user interaction by drawing differently.
make a UITableViewCell subclass to host and layout up to three of these subcells. I say up to 3 because the last cell may contain 1 or 2 subcells and not 3 if the total people to represent is not divisible by three.
Selection Logic: You'll need to override 'setSelected:' because you want to deselect and select only subcells, you don't want the whole cell to highlight on selection, only a third of it.
You'll also want to implement touchesEnded: in this cell so that you can figure out which of the three subcells was last touched, and you'll need to be able to query or communicate this back to the controller, probably using delegation. If the cell can communicate back whether selection was in subcell 0,1 or 2 then this together with the UITableViewDelegate didSelectAtIndexPath should map to your model nicely - selectedPerson = myArrayOfPeople[ (indexPath.row * 3) + subcellIndex ]
You'll be able to decorate your cells in cellForRowAtIndexPath: in similar fashion..
personOne = model.arrayOfPeople[indexPath.row*3]
personTwo = model.arrayOfPeople[indexPath.row*3 +1 ]
personThree = model.arrayOfPeople[indexPath.row*3 + 2 ]

Swift code to loop through the table custom cell rows

I am looking for code in swift to loop through the table custom cell row (have 2 text boxes in row). These rows are dynamically created and on click of button, i want to read those text box values. I don't find good example for this in online, any help is appreciated.
Your thinking is wrong. You should not store any data in table view cells. The user can scroll them off the screen at any time and any changes to the data is lost.
You should really store the input from your table view cells into your model as soon as the user makes a change.

Resources