HusseinB
HusseinB

Reputation: 1341

tagging buttons in tableview cells

In my app I have a table view. in each cell of the table view there has to be a button that should be linked to an object in a separate array. In the story board I have set the button's tag to number 6 (for configuration purposes when populating the table).

Now, in order to distinguish between the buttons that are added, in tableview:cellForRowAtIndexPath, i change the tagging of the button like this:

[(UIButton*)[cell viewWithTag:6] setTag:1000+indexPath.row];

Therefore, when a button is clicked I subtract by 1000 and know the index to which it should point in an array. The problem is when I do this tagging, only a couple of buttons get tagged and the others appear as null in code (but they do appear when I run the app).

Why is this happening? Is there a way to reference a set of buttons in a cell to an array? Note: I'm doing this not for the sake of selecting a table cell, I know I can implement that. But I have more than one button in a cell.

Here is a picture of the debugger. The PRE means before tagging and "added" after tagging a button in a cell. As you can see, the first 5 get tagged and the others start appearing as null. Note that all of those buttons are in the same section of the table. enter image description here

Upvotes: 0

Views: 1005

Answers (3)

Greg
Greg

Reputation: 25459

It's not going to work because when you call setTag the tag of your cell will be set when the view is visible but when you scroll the table view and the cell go off screen it's put to reusable pool and if the cell is viewable again it's taken from reusable pool and will have tag 6, which you have set in the storyboard. I don't know what do you try to achieve but why do you want to set tag if you know it's always 1000+indexPath.row? You should also change [cell viewWithTag:6] to [cell.contentView viewWithTag:6] you add subview to contentView not cell directly.

// EXTENDED If you want to know what button was pressed and on which cell you can add method to the button tap event like that:

- (IBAction)buttonPressed:(id)sender {
    //Button pressed
    UIButton *pressedButton = (UIButton*)sender;
    // Cell on which the button was pressed
    UITableViewCell *cell = (UITableViewCell*)[[[sender superview] superview] superview];
}

Upvotes: 1

Hani Ibrahim
Hani Ibrahim

Reputation: 1449

Your problem is because of the cell reuse

Your problem in details

You add the button tag to be 6 in your nib file or storyboard, and so when the table create a new cell it sets the button tag to be 6. Then you are able to get that cell by using

[cell viewWithTag:6]

This will work fine for the first 5 rows (or the first rows displayed before you need to scroll the table).

After you scroll the table the table then will get an "old cell" that is no longer displayed to change its properties however that you have already changed the tag of the button in that cell and so you will not be able to get the button with tag 6 as the button now has tag (1000 + x) which you have previously set that

The solution

Create class for that cell and add an IBOutlet for that button so you can access that cell with the IBOutlet like this

cell.button

Then once you have the button then you can set the tag easily

cell.button.tag = indexPath.row;

Upvotes: 0

Cy-4AH
Cy-4AH

Reputation: 4585

You are using storyboard. Why you use viewWithTag instead IBOutlet? It's sow convinient in Storyboard.

Also it's better to subclass UIButton, add in it member, that will point at item from yours array and use this pointer in button tap handler.

Upvotes: 0

Related Questions