views:

197

answers:

1

this is driving me MAD now.. I have a UItableview. Based on an NSMutableArray, I populate it. I set up in reuseTableViewCellWithIdentifier with the following

cellRectangle = CGRectMake((ARROW_OFFSET + 5), (ROW_HEIGHT - LABEL_HEIGHT) / 2.0, ARROW_WIDTH, LABEL_HEIGHT);
UIButton *tmpButton = [[UIButton alloc] initWithFrame:cellRectangle];
[tmpButton initWithFrame:cellRectangle];
[tmpButton setImage:[UIImage imageNamed:@"icon_edit.png"] forState:UIControlStateNormal];
[tmpButton setImage:[UIImage imageNamed:@"icon_no.png"] forState:UIControlStateDisabled];
[tmpButton addTarget:self action:@selector(editSelectedRow:) forControlEvents:UIControlEventTouchUpInside];  
tmpButton.tag = ARROW_TAG;
[cell.contentView addSubview: tmpButton];
[tmpButton release];

then in cellForRowAtIndexPath I have the following lines of code

UIButton *button = (UIButton *)[cell viewWithTag:ARROW_TAG];
[button setTag:indexPath.row];

if (counterHasStarted == 1) {
 NSLog(@"yes");
 button.enabled = NO;
} else {
 button.enabled = YES;
}

the button shows nicely, but for some reason when the counterHasStarted variable (which is an int is set, it doesn't change! I can change UILabels based on the above code (checking if counterHasChanged is 1 or 0).

Any ideas what's going on??

+2  A: 

-cellForRowAtIndexPath: will only get called when the table view needs a new UITableViewCell because the user scrolled.

I guess you're changing counterHasStarted and expect the button enabled state to change? You could reload the data when you change counterHasStarted ([yourTableView reloadData]). Then the table view will call -cellForRowAtIndexPath: for all the cells that are currently visible, and you can enable or disable the buttons as needed.

Thomas Müller
that's the problem, I am calling [aTableView reloadData] every second. There is a timer in each cell which gets updated successfully... it's just this button which doesn't. Must be something else in my code stopping it.... :(
Matt Facer
I had another look at your code. You first retrieve the button with tag ARROW_TAG, but then you change the tag. Could that be the problem? You change the tag, and next time your code won't find the button again? Can you verify that button is not nil, just before you try to enable/disable it. If it's nil then button.enabled = YES won't do anything but won't cause an error either. You would just be sending a message to nil.
Thomas Müller
thanks for the answer... I think you cracked it! I commented out the code to change the tag for the button at each row and it seems to have worked. I just need to now think of a way to detect the row ID when the particular button is pressed... any ideas? I was previously using the button.tag as the row ID, then grabbing that.
Matt Facer
on the note above... I found this http://www.71squared.com/2008/12/custom-button-in-uitableviewcell-indexpath/comment-page-3/#comment-2292 which talks about how to get the current row selected (so I no longer need to set anything at the button level for the row ID)
Matt Facer