views:

546

answers:

1

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?

A: 

You could just store (id)sender from the

-(void)showDeleteSheet:(id)sender

method in an instance variable.

self.curButton = sender;

then call

[curButton setBackgroundImage:image];
Joe Cannatti
Thanks Joe... this does the trick:[sender setBackgroundImage:[UIImage imageNamed:@"not-downloaded-arrow.png"] forState:UIControlStateNormal];Now... (there is always a now)... if the user clicks cancel the image is still changed because the image change happens in showDeleteSheet and not in the action sheet's clickedButtonAtIndex method. Of course there, I can't get the sender. Any thoughts?
Michael
Odd, I can set the background image but I cannot set userInteractionEnabled to no to disable the button with [sender userInteractionEnabled = NO];
Michael
you mean sender.userInteractionEnabled = NO; or [sender setUserInteractionEnabled:NO]; right ?
Joe Cannatti
to solve the cancel problem you could set self.curButton = nil; when the cancel button is touched
Joe Cannatti
I see what you mean now about saving sender in an instance variable. However, I don't know what to make the variable... can't make it an int and id doesn't help. The button's sender looks to be an array.
Michael
So to wrap up this question. I created an NSArray *buttonSender in the .h file. Then in the target of the button, made buttonSender = sender; putting the button into the array. Then in the action sheet I sent the swapImage method the array with [self swapImage:buttonSender];Thanks Joe for the pointer.
Michael