How to change height of custom cell on button's click?

I have an issue regarding height of custom cell. I need to increase the height of cell when one button on cell was clicked. her I know to use two methods (heightforrow and didselectrow) for it but I am confuse that when I clicked on button that time custom method for button action is called and I am using those two methods in controller.I attached my code:

in customcell.m

- (IBAction)btnRestPlusClicked:(id)sender
{
    UIButton *btn = (id)sender;
    btn.selected =!btn.selected;
    if (btn.selected)
    {
         NSLog(@"selected");
       // _viewExtraScheduleAmount.hidden = FALSE;//I need to make this event on button action and same time increase the height of cell.
    }
    else
    {
        btn.tag = 0;
       // _viewExtraScheduleAmount.hidden = TRUE;
    }

now I need that when button clicked that time only that row's height will increase.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //if (ceel.btnRestPlusClicked.selected) 
    //{
    //    return 100;
   // }
   // return 60;
 I know I am wrong here but how to use this method?

}

Please can any one help me out from this? Thanks

Upvotes: 1

Views: 2516

Answers (4)

TestGrid.io Team
TestGrid.io Team

Reputation: 1

You can change the height of your selected cell when you tap on that cell button like

ViewController.h 

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
    IBOutlet UITableView *myTableView;
}
@end


ViewController.m
@interface ViewController ()

@end

@implementation ViewController {

    NSArray *tableData;
    int currentselection;
    int cellno;
}

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    currentselection = -1;
    tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableData count];
}

- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath {
    int rowHeight;
    if ([indexPath row] == currentselection) {
        rowHeight = 200;
    } else {
        rowHeight = 50;
    }
    return rowHeight;
}

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

    static NSString *simpleTableIdentifier = @"SimpleTableItem";    
    NSInteger myInteger = indexPath.row;
    cellno = (int) myInteger;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];   
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    // add custom expand height button
    UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    addFriendButton.frame = CGRectMake(200.0f, 5.0f, 75.0f, 30.0f);
    [addFriendButton setTitle:@"Add" forState:UIControlStateNormal];
    [cell addSubview:addFriendButton];
    [addFriendButton addTarget:self action:@selector(addHeight:) forControlEvents:UIControlEventTouchUpInside];
    [addFriendButton setTag:cellno];

    return cell;
}

- (IBAction) addHeight:(UIButton *)sender {

    NSInteger myInteger = sender.tag;
    currentselection = (int) myInteger;
    [myTableView beginUpdates];
    [myTableView endUpdates];
}

Upvotes: 0

ruslan.musagitov
ruslan.musagitov

Reputation: 2134

Create NSMutableDictionary inside of UIViewController where you will store NSIndexPath of btnRestPlusClicked cells.

Then track when button plus selected in each cell:

In CustomCell.h

@protocol CustomCellDelegate;

@interface CustomCell : UITableViewCell    
@property (nonatomic, weak) id<CustomCellDelegate> delegate;
@end

@protocol CustomCellDelegate <NSObject>
- (void)customCellButtonPlusSelected:(CustomCell*)cell;
@end

In CustomCell.m

- (IBAction)btnRestPlusClicked:(id)sender
{
    [self.delegate customCellButtonPlusSelected:self];
}

In UIViewController when you create cell for cellForRowAtIndexPath add:

cell.delegate = self

and conform UIViewController to CustomCellDelegate

-(void)customCellButtonPlusSelected:(CustomCell*)cell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    NSString *key = [NSString stringWithFormat:@"%li:%li", indexPath.section, indexPath.row];
    [buttonPlusClickedDictionary setObject:@"1" forKey:key];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [NSString stringWithFormat:@"%li:%li", indexPath.section, indexPath.row];
    if ([buttonPlusClickedDictionary objectForKey:key]) {
        return 100;
    } 
    return 60;

}

Upvotes: 2

matt
matt

Reputation: 535401

The problem is not the implementation of heightForRowAtIndexPath: but in getting the runtime to call heightForRowAtIndexPath: again, so that it can discover that your cell height has changed. To do that, your button should tell the table view to reloadData.

Upvotes: 1

Pankaj Gaikar
Pankaj Gaikar

Reputation: 2483

Try adding a boolean variable as class member. Set it when you click the button. Reload the table view at the end of - (IBAction)btnRestPlusClicked:(id)sender

In - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath change the height of tableview cell if boolean variable is set.

Upvotes: 1

Related Questions