Pratap Manish Bhadoria
Pratap Manish Bhadoria

Reputation: 371

how to increase and resize the tableview cell in iphone?

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

In this place, i would like to add a button dynamically to increase the height of the cell, so when user clicks upon that button it should be increase the height of cell and then click again to resize the height.

I want to something like :

-(void)IncreaseCell
{
    UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [DownArrow addTarget:self 
                  action:@selector(IncreaseCell:)
        forControlEvents:UIControlEventTouchDown];
    //[DownArrow setTitle:@"Arrow" forState:UIControlStateNormal];
    DownArrow.frame = CGRectMake(121.0, 112.0, 72.0, 37.0);
    [cell.contentView addSubview:DownArrow];

    UIImage *buttonDown = [UIImage imageNamed:@"friendsDownArrowButton.png"];
    [DownArrow setBackgroundImage:buttonDown forState:UIControlStateNormal];
    [cell.contentView addSubview:DownArrow];   
}

Upvotes: 1

Views: 1135

Answers (4)

raghu_dev
raghu_dev

Reputation: 91

what i did in one of my project is i created a custom cell and add subview to it statically

1 st scene non collapsed view custom cell shows only button on click of which it will show extended view by increasing size of cell..

i.e on click of button you should increse size of cell using function..

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

 BOOL check=[[booleanArrayofrows objectAtIndex:indexPath.row] boolValue];

        if (check)
        {
            return 180; ..expanded view height
        }
        return 40; collapse view height
    } 
}

Upvotes: 0

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

All you have to do is declare a variable:

@interface yourViewController ()
{
    CGFloat cellSize;
}

And in your heightForRowAtIndexPath::

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

in your viewDidLoad

-(void)viewDidLoad
{
     [super viewDidLoad];
     cellSize = 50;
}

and last in your increaseCell:

-(void)IncreseCell {
     cellSize += 10;
     [self reloadData];
}

Upvotes: 0

Tim Bodeit
Tim Bodeit

Reputation: 9703

  1. You will need to create a NSMutableArray as an instance variable, in which you keep track of all the cells, that you want to have "increased".

    @interface YourTableViewController : UITableViewController {
        NSMutableDictionary *increasedRows;
    }
    

    Remember to alloc/init that variable.

  2. To get your cell increased:

    -(void)increseCell: (BOOL)status forIndexPath: (NSIndexPath*)indexPath {
        [increasedRows setObject:[NSNumber numberWithBool:status] forKey: indexPath];
    
        [self.tableView beginUpdates]; //Delete if you don't want it animated
        [self.tableView endUpdates]; //Delete if you don't want it animated
    
        // [self.tableView reloadData]; //Uncomment if you don't want it animated
    }
    
  3. You change your tableView: heightForRowAtIndexPath: declaration to check this dictionary for your indexPath.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        // Check if cell is increased, return custom height
        if([[increasedRows objectForKey:indexPath] boolValue]) {
            return 150;
        }
        // Cell isn't increased so return regular height
        return 50;
    }
    

This method will allow you to do it for every row individually and allows you to animate it.

Upvotes: 1

Wain
Wain

Reputation: 119041

You need to change your implementation of heightForRowAtIndexPath.

Add some storage, like an array, which holds flags indicating if a row is expanded or not. When a button is tapped, get the index path of the cell that it is on and edit the array contents to set / unset the flag. Reload the table view (or the row for the changed index path).

Upvotes: 0

Related Questions