gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:36:11 25 4
gpt4 key购买 nike

这真是难倒我了。我知道如何在 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;

}


关于ios - UITableViewCell 显示后更改其框架(不仅仅是高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19150402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com