Ios 显示UITableViewCell后更改其边框(不仅仅是高度)

Ios 显示UITableViewCell后更改其边框(不仅仅是高度),ios,objective-c,uitableview,subclass,Ios,Objective C,Uitableview,Subclass,这真让我难堪。我知道如何在heightForRowAtIndexPath中更改UITableViewCell的高度。问题是,如果我试图使两个单元格有时重叠,那么有时我需要所讨论的单元格的框架高于行的高度。我知道如何通过对自定义单元格进行子类化并重写setFrame方法来实现单元格的初始显示。但一旦单元格显示,我似乎无法手动更改帧。再一次-我不是说在这里使用heightForRowAtIndexPath或tableview.rowHeight-我是说在单元格显示后保留行高并手动更改其边框(可能多次

这真让我难堪。我知道如何在heightForRowAtIndexPath中更改UITableViewCell的高度。问题是,如果我试图使两个单元格有时重叠,那么有时我需要所讨论的单元格的框架高于行的高度。我知道如何通过对自定义单元格进行子类化并重写setFrame方法来实现单元格的初始显示。但一旦单元格显示,我似乎无法手动更改帧。再一次-我不是说在这里使用heightForRowAtIndexPath或tableview.rowHeight-我是说在单元格显示后保留行高并手动更改其边框(可能多次)。有人能帮我吗?

好的,我不知道我有没有你的问题,如果有什么遗漏,请评论,我正在发布示例代码,通过为每个单元格设置不同的帧来手动更改自定义单元格的帧,在我的代码中,通过点击按钮,每次都会更改最后一个单元格的帧。试试看,这可能是你需要的,我不知道你到底想要什么。希望这有帮助:)


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

}
//在CustomCell.h文件中
#进口
@接口CustomCell:UITableViewCell
{
}
@属性(非原子,赋值)CGRect frameRect;
@结束
//CustomCell.m文件
#导入“CustomCell.h”
@实现自定义单元
@合成frameRect;
-(id)initWithStyle:(UITableViewCellStyle)样式重用标识符:(NSString*)重用标识符
{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我){
//初始化代码
}
回归自我;
}
-(无效)解除锁定
{
[super dealoc];
}
-(无效)设置选定:(BOOL)选定动画:(BOOL)动画
{
[超级设置选定:选定动画:动画];
//为所选状态配置视图
}   
-(无效)布局子视图
{
//始终尝试在布局子视图中设置框架
[超级布局子视图];
self.frame=frameRect;
}
//在ViewController.h文件中
#进口
@界面ViewController:UIViewController
-(iAction)changeFrame:(id)发送方;
@结束
//ViewController.m文件
#导入“ViewController.h”
#导入“CustomCell.h”
@接口ViewController()//委托
{
CGRect aChangableRect;//您的可变帧
int k;
int yPos;
}
-(无效)viewDidLoad
{ 
k=25;
yPos=220;
aChangableRect=CGRectMake(10440,50,30);
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回3;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回2;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//最初为所有单元格设置“否”
CustomCell*cell=[self.aTableView dequeueReusableCellWithIdentifier:@“cell”];
如果(单元格==nil)
{
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];
}
if(indexPath.section==0)
{
if(indexPath.row==0)
{
cell.frameRect=CGRectMake(13,5150,50);
}
else if(indexPath.row==1)
{
cell.frameRect=CGRectMake(5,60,200,45);
}
}
else if(indexPath.section==1)//第二节
{
if(indexPath.row==0)
{
cell.frameRect=CGRectMake(13,110,150,50);
}
else if(indexPath.row==1)
{
cell.frameRect=CGRectMake(5165200,45);
}
}
其他的
{
//cell.frameRect=aChangableRect;//用于最后一节的最后一个单元格
if(indexPath.row==0)
{
cell.frameRect=CGRectMake(13,220,150,50);
}
else if(indexPath.row==1)
{
cell.frameRect=aChangableRect;
}
}
cell.textlab.text=@“快乐编码”;
返回单元;
}
//每次单击按钮时,我都会更改最后一个单元格的边框
-(iAction)变更框架:(id)发送方
{
aChangableRect=CGRectMake(25450,k*2,k*3);
[self.aTableView reloadData];
k=k+10;
}


改变
cell.contentView
的框架会起作用吗?不,我试过了。同时更改cell.contentView.clipsToBounds=NO.u是否使用分组表视图?是的,我正在使用分组的表格视图,不同的单元格有不同的行高。太棒了。非常感谢你!
//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;

}