Determine section index from UITableView section header or footer? - iphone

I have a multi-section table view, and each section has a button in its footer that should add a new item to that section. The number of sections is not pre-determined or limited, so I can't make a separate selector for each section like "addItemToSection1", etc.
I also can't just store the section index in the button's "tag" property since the table also supports adding or removing arbitrary sections, which changes the section indexes for all following sections.
The only thing I can think of is to maintain my own, separate map from buttons to sections or something similar, which is a lot more fiddly gruntwork than I'd like. Is there any way to determine directly what section a given header or footer is in?

Re: gerry3's answer above (Added as an "answer" because SO won't allow me to comment on the above answer for some reason.)
Thanks for the info. It seems there is no way to avoid having to maintain a separate data structure to map sections to and/or from the underlying model, since I have to have the section index so I can call things like insertRowsAtIndexPaths:withRowAnimation:, and since the section indexes can change due to adding or removing sections. How annoying!

I have seen a few methods for associating a button to its tableview cell or, more importantly, the row or data associated with that cell. You should be able to do something similar for tableview sections.
Use the button's tag. In the button's action selector, use the button's tag to get the data. It sounds like you have already ruled this out.
Use a custom view for the section which has a property for its associated data and the button as a subview. In the button's action selector, get the specific instance of your custom view as your button's parent view. Then, get the data from the custom view's property.
Put the button in an array of buttons and the data in an array of data at the same index as the corresponding button. In the button's action selector, find the index of the button in the button array and grab the data from the data array using that index.

Related

Injecting table cell at end of table

I'm completely new to swift and Xcode and everything regarding iOS development. I have a UITableView that populates UITableViewCells, I've been able to graps the fundamentals of that. But what if I wanted to add an extra cell at the bottom upon every visit?
I guess it's a mix of dynamic and static, but I can't find any answers whether this is case as of present date, since information in threads shows discrepancy.
You need to use dynamic table view. For example you have an array with some elements and your table view is displaying them. If you want to add a cell on the bottom, simply add another element to your array and then call self.tableView.reloadData()

can I show an index for UITableView (ie sectionIndexTitlesForTableView) without a searchbar?

I am trying to make an bunch of links on the right side of the uitableview to jump to the various sections in the tableview without having a searchbar at the top.
thanks
Sure can. Just implement a couple of methods in your table view’s data source: -sectionIndexTitlesForTableView:, which should return an array of titles, and -tableView:sectionForSectionIndexTitle:atIndex:, which returns the corresponding section once the user clicks on a section title.

combine two functions into a UITableViewCell: selection and information

I'm using a UITableView to present options to the user. The user can select one option by clicking on one of the cells. The concept is similar to the Radio input in html forms.
I'm looking for some ideas on how to combine two functions into a UITableViewCell.
I've taken care of the selection function. I would like to also let the user get information on a particular UITableViewCell.
The information is text that will not fit into a subtitle on the UITableViewCell.
Create mirror arrays, one with info for the selection, and one with the other information; then on selection, insert the other info into the first array in the appropriate position and display
Or
Create a footer under the tableView and display the info whenever a cell is selected.

Three20 TTSectionedDataSource row height

I'm using Three20 to create a table with several textfields for user registration. I've found two possible methods using Three20. The first uses the TTSectionedDataSource's tableDidLoadModel method to manually add UI components and the second adds custom items that contains pre formatted UI components. The second option seems way more complex and I'm having a difficult time accessing the individual fields. So if one field is a textfield for the username, I need to access the field to submit the username and it doesn't seem like there's an easy answer. The first option gives me a lot of flexibility, but I can't figure out how to set the individual row heights. One row may have a label above a text field, another may have an image, etc. Is there a method that can be used in TTSectionedDataSource that will allow me to set the height for each row? Thus far, I'm using method one and creating UIViews to hold a label field and a text field. I've tried changing the frame of the uiview before it is added to the items array, but it has no affect.
Any ideas?
I believe I may have figured it out. Not sure if this is the correct solution, but it seems to be working.
First in my custom item class I pass the datasource as a delegate. Now that the delegate is part of the item, I can pass it to my textfield as the delegate. As long as I include UITextFieldDelegate in my data source class, it will respond as the delegate to my textfield. So that's getting the content from the textfield.
If I want to change the content in a textfield from the datasource, I can leverage the method:
- (void)tableView:(UITableView*)tableView cell:(UITableViewCell*)cell willAppearAtIndexPath:(NSIndexPath*)indexPath
I can check the row using indexPath.row then type the cell as the corresponding custom cell class. From there I can access any public methods in my cell class. So I created one that returns a reference to the textfield. So I can say:
[[(MyCustomTextFieldCell *)cell theTextField] setText:#"hello world"];
Next step is to assign it to a local ivar and then I'm assuming I should be able to access it at any time.
The main reason I want to be able to modify the content of the textfield is that in certain instances by clicking on the textfield, a picker will come up and the results of the picker are formatted and inserted back into the textfield.
Please let me know if my approach is too convoluted. Perhaps I'll create a sample and post it for everyone to rip apart and tell me I'm a moron and there's a better way.
thanks,
howie

Reordering UITableView sections on the iPhone by dragging (like reordering rows)

Is there anything currently in the SDK that allows for re-ordering of tableView sections? As in, move the entire section below or above an adjacent section? This is plausible with individual UITableViewCells. Haven't seen it done for sections though.
Maybe not ideal but fairly simple so worth considering:
Add Up/Down buttons to your section headers. Down on the first, Up on the last and Up and Down on all the others. Then respond to the button presses by programmatically re-ordering the table. Up moves the section up one and Down moves the section down one.
Martin
There's no built-in touch-responsive API for moving table view sections - you'd have to do it programmatically then send a [tableView reloadData] message or similar.
It is concievable, though, that you create a table view where each UITableViewCell's view is itself a UITableView containing a section of your data, so that the cells in the "master" table are draggable as UITableViewCells. This would let you reorder "sections" in your table, but they wouldn't be sections anymore - they'd be separate tables, each with a single section.
Maybe as an alternative idea, reloadData on the table to only show the sections as a kind of "collapsed all" view on your table (maybe it is possible to even animate this?) which then has one section, containing all sections as row elements. The only extra required would then be a button that will set your table to this state.
I tried to move rows from one section to another, while updating the datasource of course. But this seemed to be mission impossible. Empty cells in between, wrong contents in sections, wrong ordering of sections were the only results I got.