bill bill - 2 months ago 13
iOS Question

How to increase or expand the row height of each cell when clicking inside the custom table view cell in ios, objective c


  • here is my problem , I have a
    table veiw
    which is works fine.

  • I have used a
    custom tableview cell
    . Inside that
    custom table view cell
    there is a
    button
    .

  • default, I have set the
    row height
    to
    300
    using the
    heightforrowatindexpath
    method.





  • Now what I want is to expand/ increase the each row height to 400 , when user tap on the button inside the custom tableviewcell. (need to expand the height only for the tapped cell).




Below is my implementation of the tableview in .m file.

#import "ViewController.h"
#import "FirstTableViewCell.h"

@interface ViewController ()

@end

@implementation ViewController
{
NSArray *bbarray;

}

@synthesize buttonPlusClickedDictionary;

- (void)viewDidLoad {
[super viewDidLoad];

bbarray = [NSArray arrayWithObjects:@"33",@"44",@"33",@"44",@"33",@"44",@"33",@"44",@"33",@"44", nil];
// Do any additional setup after loading the view, typically from a nib.
}

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:@"tvcell"];
[cells setColletionData:bbarray];

[cells.expandMe addTarget:self action:@selector(runs:) forControlEvents:UIControlEventTouchUpInside];


return cells;
}

- (IBAction)runs:(id)sender{

[self.maintableView beginUpdates];
[self.maintableView endUpdates];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 300;
}


@end


how can I do that. some answers had to use
[self.tableview beginUpdates]
and
[self.tableview endUpdates]
, I do not know how to use those.

Any help will be appreciated.

note : ehen I click on the same button second time, I need the previos row height

Answer

Try this..

@implementation ViewController
{
    NSArray *bbarray;
    long tagForExpandOfRow;
}

@synthesize buttonPlusClickedDictionary;

- (void)viewDidLoad {
    [super viewDidLoad];

   tagForExpandOfRow = -1;

    bbarray = [NSArray arrayWithObjects:@"33",@"44",@"33",@"44",@"33",@"44",@"33",@"44",@"33",@"44", nil];
    // Do any additional setup after loading the view, typically from a nib.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:@"tvcell"];
    [cells setColletionData:bbarray];

    [cells.expandMe addTarget:self action:@selector(runs:) forControlEvents:UIControlEventTouchUpInside];
     cells.expandMe.tag = indexPath.row;

    return cells;
}

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

    if (tagForExpandOfRow == sender.tag)
        tagForExpandOfRow = -1;
    else
        tagForExpandOfRow = sender.tag;

    [self.maintableView reloadData];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tagForExpandOfRow == indexPath.row)
       return 400;
    else
       return 300;
}
Comments