SAHM
SAHM

Reputation: 4179

Changing frame (not just height) of UITableViewCell once it has displayed

This is really stumping me. I know how to change the height of a UITableViewCell in heightForRowAtIndexPath. The issue if that I am trying to get two cells to sometimes overlap - so sometimes I need the frame of the cell in question to be higher than the height of the row. I know how to do this for the initial display of the cell by subclassing the custom cell and overriding the setFrame method. But once the cell displays, it seems that I cannot manually change the frame. Again - I am not talking about using heightForRowAtIndexPath here or tableview.rowHeight - I am talking about leaving the row height and manually changing the frame of the cell (possibly multiple times) after it has been displayed. Can anyone help me out?

Upvotes: 0

Views: 962

Answers (1)

Shankar BS
Shankar BS

Reputation: 8402

Ok i dont no i got ur question or not, comment if there is any thing missed, i am posting the sample code to change the frame of custom cell manually by setting the different frame for each cell, in my code by tapping a button the frame of last cell is changed each time. Try it out this might u needed i dont no what u exactly want. Hope this Helps :)


//in CustomCell.h file

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell
{


}

@property(nonatomic, assign) CGRect frameRect;
@end


//CustomCell.m file
#import "CustomCell.h"

@implementation CustomCell
@synthesize frameRect;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {
    // Initialization code

    }
  return self;
}
- (void)dealloc
{
    [super dealloc];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
   [super setSelected:selected animated:animated];

   // Configure the view for the selected state
}   

- (void)layoutSubviews
 {
   // always try to set frame in layoutSubviews
    [super layoutSubviews];
    self.frame = frameRect;

}

 //in ViewController.h file

 #import <UIKit/UIKit.h>


 @interface ViewController : UIViewController<CountdownTimerDelegate>

 - (IBAction)changeFrame:(id)sender;
 @end

 //ViewController.m file

 #import "ViewController.h"
 #import "CustomCell.h"

 @interface ViewController ()<UITableViewDataSource,   UITableViewDelegate>//delegate
 {
    CGRect aChangableRect; //your changeable frame
    int k;
    int yPos;
 }

 - (void)viewDidLoad
 { 
    k = 25;
    yPos = 220;
    aChangableRect = CGRectMake(10,440, 50, 30); 
 }

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

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

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     //initially set it no for all cell
    CustomCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell == nil)
     {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
     }

   if(indexPath.section == 0)
   {
        if(indexPath.row == 0)
            {
                cell.frameRect = CGRectMake(13, 5, 150, 50);
            }
        else if (indexPath.row == 1)
        {
            cell.frameRect = CGRectMake(5,60 , 200, 45);
        }
  }
 else if(indexPath.section == 1) //second section
  {
    if(indexPath.row == 0)
    {
        cell.frameRect = CGRectMake(13, 110, 150, 50);
    }
    else if (indexPath.row == 1)
    {
        cell.frameRect = CGRectMake(5,165 , 200, 45);
    }

 }
 else
  {
     // cell.frameRect = aChangableRect; //for last cell of last section
    if(indexPath.row == 0)
    {
        cell.frameRect = CGRectMake(13, 220, 150, 50);
    }
    else if (indexPath.row == 1)
    {
        cell.frameRect = aChangableRect;
    }

  }


   cell.textLabel.text = @"happy coding";
   return cell;
}

 //i am cganging the frame of last cell each time button clicked
 - (IBAction)changeFrame:(id)sender
 {

    aChangableRect = CGRectMake(25, 450  , k * 2, k * 3);
    [self.aTableView reloadData];
    k = k + 10;

}


Upvotes: 1

Related Questions