我想在tableview的选定单元格上添加一个视图。我正在写这段代码。
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
myView.backgroundColor=[UIColor greenColor];
myView.tag=1001;
[cell.contentView addSubview:myView];
}
编写此代码后,由于此视图,我的其他单元格未显示。我想将所选单元格的高度增加到200,并在所选单元格上添加自定义视图。
任何人都可以告诉我该怎么做, 提前谢谢。
答案 0 :(得分:0)
创建所选单元格的属性
@property (nonatomic) int currentSelection;
然后在这里初始化
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.currentSelection = -1;
}
在heightForRowAtIndexPath中,您可以为所选单元格设置所需的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
int rowHeight;
if ([indexPath row] == self.currentSelection) {
rowHeight = 200;
}
else
rowHeight = CURRENT_HEIGHT_HERE;
return rowHeight;
}
然后在此处添加您的视图并将其隐藏为:
//自定义表格视图单元格的外观。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
myView.backgroundColor=[UIColor greenColor];
myView.tag=1001;
[myView setHidden:YES];
[cell.contentView addSubview:myView];
// Configure the cell.
return cell;
}
在didSelectRow中,您可以保存当前选择并保存动态高度(如果需要)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// do things with your cell here
// set selection
self.currentSelection = indexPath.row;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[[cell viewWithTag:1001] setHidden:NO];
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
答案 1 :(得分:0)
好的,因为我告诉你,你需要在下面的例子中对UITableViewCell进行子类化,当选择单元格时,它会将自定义视图添加到选定的单元格中,你可以根据需要进行更改。希望这有助于你:)
//in subclassed cell "CustomCell.h" file
@interface CustomCell : UITableViewCell
{
UIView *aView; //to hold ur view
BOOL addView; //to check wheather to add view or not
}
@property(nonatomic, retain) UIView *aView;
@property(nonatomic, assign) BOOL addView;
@end
//in "CustomCell.m" file
#import "CustomCell.h"
@implementation CustomCell
@synthesize aView;
@synthesize addView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
aView = [[UIView alloc]initWithFrame:CGRectZero]; //init the view with zero rect as if u want initially frame is zero
aView.backgroundColor = [UIColor greenColor]; //to know weather it is added or not
[self addSubview:aView];//added to cell
}
return self;
}
- (void)dealloc
{
[aView release];
[super dealloc];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
if(self.addView) //if the view is added
{
self.aView.frame = CGRectMake(10, 3, 100,200);
}
else
{
self.aView.frame = CGRectZero;//if there is no view
}
}
//in your ViewController.h file
@interface ViewController : UIViewController<CountdownTimerDelegate>
@property (retain, nonatomic) IBOutlet UITableView *aTableView;
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
@end
//in ViewController.m file
#import"CustomCell" //add this to header file
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [self.aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell =[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(self.selectedIndexPath != nil)
{
if(self.selectedIndexPath.row == indexPath.row)
cell.addView = YES;
else
cell.addView = NO;
}
//initially set it no for all cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//in did select row at indexpath
self.selectedIndexPath = indexPath;
[self.aTableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.selectedIndexPath != nil)
{
if(self.selectedIndexPath.row == indexPath.row)
{
return 200;
}
return 45;
}
else
return 45;
}
答案 2 :(得分:0)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedIndex = indexPath.row;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
myView.backgroundColor = [UIColor greenColor];
myView.tag = 1001;
[cell.contentView addSubview:myView];
[tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([indexPath row] == selectedIndex) {
return myView.bound.size.height;
}
else
return currentHeight;
}
希望这会奏效。