views:

214

answers:

2

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?

A: 

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.

  1. 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.
  2. 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.
  3. 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.
gerry3
A: 

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!

Michael