buttons in UITableview

Discussion in 'iOS Programming' started by mmalsn, Aug 18, 2011.

  1. mmalsn macrumors newbie

    Joined:
    Jul 20, 2011
    #1
    hi guys

    I have Tableview an of course it has cells

    and I add 3 buttons for each cell and one event for all cell


    the problem is i want to know which button is being selected to open a screen according to this value

    Thanks.
     
  2. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #2
    Give the buttons a tag each and check the returned tag from inside the assigned action

    - Olaf
     
  3. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #3
    A couple of questions pop into my head immediately:
    • How many rows?
    If it's a bunch of rows (that is, > 1 screen full of rows), then the rows will exit the screen and be put back into the reuse pool (if you're taking advantage of that). That means that the row will not have a fixed mapping to buttons. Think about how you can solve the mapping between a button press and which row & button was pressed.
    • How do the buttons get into the cells?
    This is a dual to the above problem - once you pull a cell from the reuse pool, how can you ensure the correct button is put into that cell? Do they come with because you're instantiating a UITableViewCell subclass from a nib?
    If I were doing this, I'd have to fiddle with the data structures for a little bit. I'd probably start with a couple of (lazy instantiated) mappings between buttons, indexPaths, and actions (push a view controller being one of those actions). Something like a buttonToIndexPath and indexPathToButton dictionary for the cross relationship and buttonToAction dictionary.

    When constructing a cell, I'd put the appropriate button objects for the requested indexPath into the cell's contentView, getting the objects from the dictionary mentioned above.

    Each of the button's action points to the same method (-(IBAction)buttonPress: (id)theButton) where I'd use theButton to figure out WHICH button was pressed by interrogating the dictionaries I created earlier. I could also see 3 different action methods (button1pressed, button2pressed, button3pressed) with code specific to each kind of button (so I wouldn't have to make the mapping between button and action), but the code would still likely have to know the indexPath associated with the button.

    I tend to go to over-sophisticated on data structures, so I'm likely missing a simpler version of this.
     

Share This Page