PALAK iOS Developer PALAK iOS Developer - 4 months ago 19
iOS Question

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

Answer

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;

}
Comments