S.J
S.J

Reputation: 3071

How to overcome UITableViewCell subclass limitation

I have a custom uitableviewcell and subclassed, and it is containing a uitextfield and delegate is also set, now when return key on keyboard is pressed I want to try few things

  1. perform a segue(but issue is I am in uitableviewcell subclass).
  2. modally present another view controller(but issue is uitableviewcell do not allow this).
  3. I want to display uiactionsheet(but again limitation is uitableviewcell).

If i get rootviewcontroller reference then rootviewcontroller's view itself not displayed or not the active view so any thing you do will not present on screen, active view is required.

Upvotes: 0

Views: 117

Answers (3)

Aaron
Aaron

Reputation: 7145

You could use a block property on your cell that is fired whenever your custom button action occurs. Your cell's block property might look something like this:

@interface CustomTableViewCell : UITableViewCell

@property (nonatomic, copy) void (^customActionBlock)();

@end

Your cell would then invoke this block from the custom button action like this:

@implementation CustomTableViewCell

- (IBAction)buttonTapped:(id)sender {
    if ( self.customActionBlock ) {
        self.customActionBlock();
    }
}

@end

Then finally, you set the block in -cellForRowAtIndexPath: back in your view controller (or wherever) like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
    cell.textLabel.text = [self.colors objectAtIndex:indexPath.row];
    cell.customActionBlock = ^{
        NSLog(@"Do the stuff!");
        // present view controller modally
        // present an action sheet
        // etc....
    };
    return cell;
}

One word of caution, though. If you use blocks you run the risk of strongly referencing self and creating a memory leak. Blocks are fun and easy to use but you have to play by their rules. Here are some resources to help you get familiar with them:

Upvotes: 2

BHASKAR
BHASKAR

Reputation: 1201

Implement button action in Tableview SuperClass. Or You can use Custom delegate in UITableViewCell subclass. In UITableView Subclass declare a protocol.

@protocol customCellDelegate <NSObject>

@required
-(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath;
@end

Set this property in UITableView Subclass

@property (nonatomic, strong)NSIndexPath *indexpath;
@property (nonatomic, strong) id <customCellDelegate> delegate;

And then in Your UITableView Subclass Button action add This lines

 if(self.delegate){
    [self.delegate selectedButtonInIndexPath: self.indexpath];
 }

And in your tableview datasource method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Implement this code

cell.delegate = (id)self;
cell.indexpath = indexPath;

And in Uitableview super class just implement this method

 -(void)selectedButtonInIndexPath : (NSIndexPath *)indexpath{
    //display uiimagepickercontroller modally or anything else here
 }

Upvotes: 1

meda
meda

Reputation: 45500

You can attach action to your buttons even if they are in a tableView

[cell.button addTarget:self action:@selector(presentController:) forControlEvents:UIControlEventTouchUpInside];

presentController is referring to an IBAction

- (IBAction)presentController:(id)sender
{
  //present
}

Upvotes: 1

Related Questions