简体   繁体   English

使用NSNotification从非tableview主机类更改自定义tableview单元格按钮背景图像

[英]Changing custom tableview cell button background image using NSNotification from non-tableview host class

1) I have a simple table view hosted by view controller and cell hosted by custom UITableViewCell class. 1)我有一个简单的表视图,由视图控制器托管,而单元格则由自定义UITableViewCell类托管。 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 ). 我单元格上的按钮会下拉菜单(使用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 . 2)问题是我想更新下拉菜单行选择上的按钮背景图像,这涉及到我的“超出图片的下拉菜单tableview类”和“自定义表视图单元格类”,使我的自定义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. 3)我尝试使用NSNotification类的方法,我成功地完成了一个简单的场景,仅涉及主机类和下拉菜单类,但是现在它涉及到我想交流的自定义表格视图单元(它是重复实体)和下拉类。 I set up NSNotification but background image stays the same, means notification doesn't reach/doesn't reach in time. 我设置了NSNotification但是背景图片保持不变,这意味着通知没有及时到达/未及时到达。

4) Apparently I need 10 reputation to post image (:-[) so here's the link: 4)显然我需要10个声誉才能发布图像(:-[),因此这里是链接:

在此处输入图片说明

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. 如图所示,我已经在下拉菜单的didSelectRow上触发了通知,当按下“隐藏”按钮时,背景应该更改,否则,如果按了显示,则应该如图所示为绿色..就像我之前所做的那样,但这对我没有任何帮助。 Any help will be greatly appreciated. 任何帮助将不胜感激。 thank you in advance! 先感谢您!

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 比在tableView: didSelectRowAtIndexPath:调用此块

- (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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM