anshuman
anshuman

Reputation: 131

UITableViewCell button not getting tag when new row inserted

i have created a tableview in which we can add and delete rows. On each row there is a button having tag as that of the indexPath. Now when i insert a new row above any row then that the button in that cell gets the correct tag, but the other cell-buttons retains there old tag.

I tried 1. reloading the tableview - not working 2. reloading the section - not working 3. reloading rows at all the indexPaths - not working

I used foll code to insert the new row but the cellForRowAtInsexPath method gets called only once, i.e only for the newly added row, due to which the tag of the button on old cell is remaining the same

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowNo inSection:0];
NSArray *otherArray = [NSArray arrayWithObject:indexPath];
[tableViewObj insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationNone];

and in cellForRowAtIndexPath:(NSIndexPath *)indexPath i have a button infoButton whose tag is set a per the indexPath infoButton.tag = indexPath.row;

Upvotes: 4

Views: 868

Answers (2)

jrturton
jrturton

Reputation: 119242

Yet another reason why tags in your table view cells are the wrong way to work out what button has been pressed. See my answer here: https://stackoverflow.com/a/9274863/852828

Upvotes: 3

flavianatill
flavianatill

Reputation: 484

If you are using the method "dequeueReusableCellWithIdentifier:" when creating your cells, that might be the reason why the button tags do not change.

Upvotes: 1

Related Questions