这真的难倒我。我知道如何在 heightForRowAtIndexPath 中更改 UITableViewCell 的高度。如果我试图让两个单元格有时重叠的问题 - 所以有时我需要有问题的单元格的框架高于行的高度。我知道如何通过子类化自定义单元格并覆盖 setFrame 方法来为单元格的初始显示执行此操作。但是一旦单元格显示,似乎我无法手动更改框架。再一次 - 我不是在谈论在这里使用 heightForRowAtIndexPath 或 tableview.rowHeight - 我是在谈论离开行高并在显示后手动更改单元格的框架(可能多次)。谁能帮我吗?
问问题
896 次
1 回答
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;
}
于 2013-10-03T05:17:29.443 回答