0

1) I have a simple table view hosted by view controller and cell hosted by custom UITableViewCell class. A button on my cell drops down a menu (a simple table view controller loading from the non-host 'out of the picture' class using FPPopoverMenu).

2) The problem is I want to update my button background image on dropdown menu row selection which involves my 'out of the picture dropdown menu tableview class' and my 'custom table view cell class' totally deserting the host of my custom UITableViewCell.

3) I tried using NSNotification like, I successfully did for a simple scenario involving only host-class and dropdown menu class but now its the custom tableview cell (which is a repeating entity) and dropdown class I want to communicate.. Please help. I set up NSNotification but background image stays the same, means notification doesn't reach/doesn't reach in time.

4) Apparently I need 10 reputation to post image (:-[) so here's the link:

enter image description here

As shown by the image, I have fired notification on dropdown's didSelectRow, when Hide is pressed, background should change otherwise, if show is pressed it should be green as shown..as i did before but this doesn't do anything for me. Any help will be greatly appreciated. thank you in advance!

NSPratik
  • 4,714
  • 7
  • 51
  • 81
Harris
  • 310
  • 3
  • 13
  • Can you post some code on how you update the button background right now? I mean, how you access the cell ? – NSPratik Dec 23 '15 at 08:16
  • [[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeButtonBackgr:) name:@"changePic" object:nil]; – Harris Dec 23 '15 at 09:15
  • simply in awake view from nib i registered for notification – Harris Dec 23 '15 at 09:15
  • and after requisite setup i posted notification from didSelectRowAtIndex of my popover menu tableView class – Harris Dec 23 '15 at 09:17
  • What you are doing inside `ChangeButtonBackgr` ? And is it getting called ? And yes, don't add code in comment. Please edit your question and post code there only.. – NSPratik Dec 23 '15 at 09:22

1 Answers1

1

To achieve this you can use blocks.

You need to add

@property (nonatomic, copy) void (^didSelectAction)(NSIndexPath *indexPath);

to view controller which is shown in popover.

than in tableView: didSelectRowAtIndexPath: call this block

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.didSelectAction)
        self.didSelectAction(indexPath);
}

So when you create a popover you should provide additional handler. Something like this

Add new action to your button

- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    [[cell button] addTarget:self action:@selector(showPopoverFromButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void) showPopoverFromButton:(UIButton *)sender {
    //Your table view which is shown in popover
    UITableViewController *controller = [[UITableViewController alloc] init];
    [controller setDidSelectAction:^{
         [sender setBackgroundColor:[UIColor redColor]];
    }];
    FPPopoverMenu *popover = [[FPPopoverController alloc] initWithViewController:controller];        
    [popover show];
}
Moonkid
  • 881
  • 7
  • 17
  • looks like a solution i'll check!! – Harris Dec 23 '15 at 09:41
  • the thing is, read point 2) of the question.. i think the only way is protocols and delegates... bcoz the host of cell is not being involved here – Harris Dec 23 '15 at 09:58
  • I have updated answer. As I understand you handle button action in your cell class? And in my approach the viewController is handler. – Moonkid Dec 23 '15 at 10:41
  • exactly!! so is this approach fine to try now? – Harris Dec 23 '15 at 11:03
  • please confirm what u meant by : You need to add @property (nonatomic, copy) void (^didSelectAction)(NSIndexPath *indexPath); property to popover table view. popover table view is the actual FPPopoverController or the table view that loads as FPPopover?(the one showing below button in image) – Harris Dec 23 '15 at 11:05
  • FPPopoverController doesn't have a table view :) So i meant the one showing below button in image. And again I will update my answer to clarify everything – Moonkid Dec 23 '15 at 11:13
  • i gave each cell the selector and tag in cellForRowAtIndex and used the tag to update specifically the button background in cell which's button was tapped using a check against indexPath.row and sender.tag. worked perfectly! – Harris Jan 14 '16 at 11:37