Change a UIButton View's background upon press
Posted
by Michael
on Stack Overflow
See other posts from Stack Overflow
or by Michael
Published on 2010-04-22T02:30:16Z
Indexed on
2010/04/22
2:33 UTC
Read the original article
Hit count: 358
This should be easy but its got me stumped. I've got a button on each row of a table cell. The button is to delete the file associated with that row. I have an image that indicates if the file is present on the iPhone or not present.
When the user presses the button, the target action method (showDeleteSheet) then calls a UIActionSheet with a variable to give it the indexPath of the row pressed. Then the user presses delete on the action sheet and the action sheet's clickedButtonAtIndex method deletes the file.
Now, I need to update the button's background image property to change the image in the appropriate table cell to the not downloaded image. This can be done in either the button's target action method or the action sheet's clickedButtonAtIndex method.
Here is some code:
In the table's cellForRowAtIndexPath method I create the button:
UIButton *deleteButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[deleteButton addTarget:self action:@selector(showDeleteSheet:) forControlEvents:UIControlEventTouchDown];
deleteButton.frame = CGRectMake(246, 26, 30, 30);
if (![AppData isDownloaded:[dets objectForKey:@"fileName"]]) {
[deleteButton setBackgroundImage:notdownloadedImage forState:UIControlStateNormal];
} else {
[deleteButton setBackgroundImage:notdownloadedImage forState:UIControlStateNormal];
}
In the button's target method:
-(void)showDeleteSheet:(id)sender {
//get the cell row that the button is in
NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSInteger currentSection = indexPath.section;
NSInteger currentIndexRow = indexPath.row;
NSLog(@"section = %i row = %i", currentSection, currentIndexRow);
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[table reloadData];
//if deleteButton is declared in .h then the other instances of deleteButton show 'local declaration hides instance variable'
[deleteButton setBackgroundImage:[UIImage imageNamed:@"not-downloaded.png"] forState:UIControlStateNormal];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Delete this file?"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete"
otherButtonTitles:nil];
actionSheet.tag = currentIndexRow;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
[actionSheet release];
}
Perhaps the issue is that the call to [deleteButton setBackgroundImage...]; doesn't know which cell's button should be updated. If so I don't know how to tell it which.
Because I test if the file is downloaded when the button is made and the background image is set, when I scroll down so the cell in question is de-queued then scroll back up it show's the correct image.
I've tried to force the table to reload but [table reloadData]; is doing nothing. I've tried reloadRowsAtIndexPaths and still no good.
Any one care to educate me on how this is done?
© Stack Overflow or respective owner