iOS selecting cell - ios

This might be really easy to answer but I've searched the net, stack and books but cannot find what I need.
Basically I have a really noddy app with a table, the table has 5 sections and each section has about 5 rows, all I need is for some code to put in the Didselectrow procedure to tell me what cell was selected and then do something -
so if cell 2 then display map etc.

The selected cell is indicated by the index path parameter that you get in -tableView:didSelectRowAtIndexPath:. There's a category on NSIndexPath that gives you -section and -row methods; use those to determine the selected row and then take whatever action ou want based on that.

Related

Filter tableview based on an item in another tableview

I have a UITableView displaying comments which are dynamically displayed through JSON. On the same page, I have another UITableView as a Filter.
The filter displays a list of the types of comments. I need to filter the main tableview based on the type of comment selected from the filter tableview.
Filter TableView1 based on item selected in TableView2.
TableView1 - Main tableview
TableView2 - Filter tableview
Following is an image of the page:
For instance, if I select "Worst Moments" in TableView2, TableView1 should display only those rows that contain the comment with "Worst Moments" as a keyword in it.
Can anyone help me with a code, how to filter items in TableView1 based on an item selected in TableView2?
Can anyone help me with a code
if you are asking us to code it for you then aside from the most generous souls here, most likely not.
You are almost there. This is how I would approach the problem
What you need to do is add UISearchBar to your ViewController
When an option is selected in TableView2 make it into a string
Pass that string to UISearchBar
Use that to refresh your TableView1
If you need some help you can either google UITableView & UISearchBar or you can check this Tutorial HERE.

Cyclic loading of custom cells Swift

I've been trying to get some response from stack on this issue for a while now with no success.
So i will try to be very precise.
I have an uitableview with two custom cell nibs. Data source for both are two separate arrays (questions array and answers array).
The first custom cell contains uilabel (question cell) and another one uibutton (answer cell).
On launch of the app a specific amount of question cells load up (depending on the amount of objects in the array) and after them the 1 answer cell loads up.
Here is the pic
after click answer2 - again the same procedure happens producing new question cells below as well as the answer3 cell
Don't know how to achieve that.
Thank you!
This is not that hard as it seems. So, on the launch of the app, you load the initial questions and answers. For that you would need the number of required cells, say, 3 questions and 1 answer, totally you need to return 4 from the numberOfCellsInSection:. And, when the user taps(selects, press) the answer row, you just increase the number of cells on the table, if you have 2 questions and 1 answer on the next cycle, now you would return 7 from the numberOfCellsInSection: datasource method of the tableView. And so on.
Also, you can divide the each question-answer into the sections, if you want to have more organized way of achieving your goal. In this case, you have to increase the number of sections in the table, and return proper number of cells for each section on answer cell selection. Use numberOfSectionsInTableview: and numberOfCellsInSection methods of the datasource for this.
Don't forget to draw (set proper texts to the cell's labels, image if any) the data on cellForRowAtIndexPath: method to reflect the correct data on your cells.

Toggle showing/hiding child table cells iOS

I'm trying to create a UITableview that contains STATES and CITIES. To start with, I'd like the table to display only the STATES, with each value having a downward facing disclosure arrow on the right hand side of the cell which lets the user know it has values underneath it. If the user clicks the arrow, the table would expand to show the CITIES associated with the selected STATE. The user could then either select a CITY, or click on a upward facing disclosure arrow which would then "hide" the CITIES.
I've downloaded and reviewed Apples "Table View Animations and Gestures" example.
I was hoping someone might know a simpler way of accomplishing what I'm asking for.
http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html
Good Job on explaining exactly what you want to do. This is how I would tackle this, I figure it would be easier to explain it without giving you meaningless code and answer any coding concerns you might have after.
I would set up the table view data source to be an array of arrays. The idea would be that every index in the array would represent a state and therefore contain a number of cities, hence ever index contains an array (array of cities). The parent array(of states) will be empty at the beginning, but the arrays(of cities) that will later fill it would be populated with the appropriate cities.
I would then set up the table view to contain sections (I a tempted to put code here, but read along). The sections would represent the cities. You can then fill out the table view with sections using (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section delegate. On the first run you can fill the actual table with one cell per section (maybe with the word "expand" or "more" and your down arrow). When the user touches that cell you can use the didselectrowatindexpath to know which section was touched (the indexpath object contains the section), now that you know which section it is, just modify the data source by adding the array of cities for that section of the array of arrays (parent array) and update the tableview.
This essentially gives your tableview an according style and feel. It is very easy to implement but requires a little bit of code. I have done this and am willing to provide the code you might need, the trickiest part would be to update the tableview in the correct way.
EDIT CODE:
I did a simple example following above explanation. The code is on Github , feel free to ask any questions about it.
I think a better way to represent this information would be to have a UITableView containing the states with each state showing a tiny arrow pointing to the right. Then, when a user clicks on the state, load the next UITableView that shows all of the cities in that particular state. Using a UIViewController for pushing the City list will allow users to easily return to the States list.
This approach will make much more sense to iOS Users, because this is how they expect Tables to work.
There are many tutorials explaining how to use UITableView. Here is a link to a site with many UITableView tutorials. The tutorial I linked to explains pushing a UIViewController on to the stack so that there is a simple back button back to the State list.
Normally such thing is done by seguing to another view and showing more details about cell.
What you want you could achieve by actually making custom UITableViewCell which would contain UIButton with arrow image and UITableView. In case button is clicked for the first time you could reload your cell and create inner UITableView which could show cities. Another click would simply reload cell again and not return inner UITableView at all. You also need to keep selected state somewhere because you may have to reload previous cell and hide inner table in case arrow in other cell is clicked.

Getting data from a custom cell

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.

How can I get multi selected cell items for NSArray?

I've implemented multi selected UITableView.
I want to get multi selected cell informations.
For example, there are 5 cell items in myTableView.
and I touched 3 cell items. and then I want to remove these items.
(It likes multi delete of iPhone mail)
How can i get this information?
This blog goes over it pretty well: http://cocoawithlove.com/2009/01/multiple-row-selection-and-editing-in.html
I won't paste in the code because it is significant, but I'm sure people would like it if you posted your implementation in your question as an edit after you are done.

Resources