UITableViewCell显示后更改框架(不仅仅是高度)

时间:2013-10-03 03:48:26

标签: ios objective-c uitableview subclass

这真的让我很难过。我知道如何在heightForRowAtIndexPath中更改UITableViewCell的高度。问题是,如果我试图让两个单元格有时重叠 - 所以有时我需要有问题的单元格的框架高于行的高度。我知道如何通过继承自定义单元格并覆盖setFrame方法来初始显示单元格。但是一旦显示单元格,我似乎无法手动更改框架。再说一遍 - 我不是在谈论在这里使用heightForRowAtIndexPath或tableview.rowHeight - 我说的是在显示行高后并手动更改单元格的框架(可能多次)。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

好吧我不,不管我是不是有问题,请评论是否有任何遗漏,我发布示例代码,通过设置每个单元格的不同框架手动更改自定义单元格的框架,在我的代码中通过点击一个按钮,每次更改最后一个单元格的框架。尝试一下这可能你需要我不是你想要的。希望这有助于:)


//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;

}