简体   繁体   中英

Performing action in custom Table View Cell won't work

I have a custom table view cell class. In the cell I have a UIButton . When I click on that button in the cell I want to display a popup view, in fact I'm using RNBlurModalView .

Because it's a UITableViewCell -class I can't use self , so I tried to create a UIViewController inside the custom cell, like this:

__weak UIViewController *ctrl;

And the code to display the popup like this:

-(IBAction)openView {
    RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:ctrl title:@"Title" message:@"Hello"];
    [modal show];
}

So instead of initWithViewController:self I'm trying to use initWithViewController:ctrl . But nothing happens when I click on the button.

How can I solve this?

The custom tableView cell is a subclass of UIView , and you want to show a UIViewController in it is not right. You have to tell the viewController which contains the current tableView to do the work for you. I will suggest you yo use delegate pattern to pass the message to viewController. Such as:

cell.delegate = self; // self is the current view controller

implementation the delegate method

- (void)showTheModalView
{
   RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:ctrl title:@"Title" message:@"Hello"];
   [modal show];
}

and in cell implementation

-(IBAction)openView {
//RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:ctrl title:@"Title" message:@"Hello"];
//[modal show];
[self.delegate showTheModalView];

}

Declared delegate

@protocol SomeDelegate <NSObject>
- (void)showTheModalView;
@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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