Ios 展开并折叠tableview行
我正在开发一个iOS应用程序。我有一个可展开和折叠的桌面视图。单击tableview的单元格时,tableview单元格会正确展开,但当我单击第二个单元格时,我希望第一个单元格折叠,第二个单元格展开。这样一次只能扩展一个单元格 我正在使用HVTableview类:- 代码:-Ios 展开并折叠tableview行,ios,tableview,Ios,Tableview,我正在开发一个iOS应用程序。我有一个可展开和折叠的桌面视图。单击tableview的单元格时,tableview单元格会正确展开,但当我单击第二个单元格时,我希望第一个单元格折叠,第二个单元格展开。这样一次只能扩展一个单元格 我正在使用HVTableview类:- 代码:- -(void)tableView:(UITableView *)tableView expandCell: (UITableViewCell*)cell withIndexPath:(NSIndexPat
-(void)tableView:(UITableView *)tableView expandCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
{
UILabel *detail = (UILabel *)[cell viewWithTag:3];
UIButton *button = (UIButton *)[cell viewWithTag:10];
button.alpha = 0;
button.hidden = NO;
[UIView animateWithDuration:.5 animations:^{
detail.text = @"This is Expand.";
button.alpha = 1;
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(3.14);
}];
}
-(void)tableView:(UITableView *)tableView collapseCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath;
{
UILabel *detail = (UILabel *)[cell viewWithTag:3];
UIButton *button = (UIButton *)[cell viewWithTag:10];
button.alpha = 0;
[UIView animateWithDuration:.5 animations:^{
detail.text = @"This is Collapse.";
button.alpha = 0;
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(-3.14);
} completion:^(BOOL finished) {
button.hidden = YES;
}];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_cites count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded: (BOOL)isExpanded;
{
if (isExpanded) {
return 150;
}else
{
return 100;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded;
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *title = (UILabel *)[cell viewWithTag:2];
UILabel *detail = (UILabel *)[cell viewWithTag:3];
UIImageView *image = (UIImageView *)[cell viewWithTag:1];
UIButton *button = (UIButton *)[cell viewWithTag:10];
[button addTarget:self action:@selector(goToTop) forControlEvents:UIControlEventTouchUpInside];
title.text = [_cites objectAtIndex:indexPath.row % 9];
NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
NSString* imageFileName = [NSString stringWithFormat:@"%d.jpg", indexPath.row % 9 + 1];
image.image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, imageFileName]];
if (indexPath.row %2 ==1)
cell.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:1];
else
cell.backgroundColor = [UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1];
if (!isExpanded)
{
detail.text = @"This is collapse";
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(0);
button.hidden = YES;
}
else
{
detail.text = @"This is Expand";
[cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(3.14);
button.hidden = NO;
}
return cell;
}
在UITableViewDelegate方法中:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([expandedCells containsObject:indexPath])
{
return kExpandedCellHeight; //It's not necessary a constant, though
}
else
{
return kNormalCellHeigh; //Again not necessary a constant
}
}
这里的关键是确定是否应该展开/折叠单元格,并在委托方法中返回正确的高度。因此,相应地设计您的需求
在UITableViewDelegate方法中:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([expandedCells containsObject:indexPath])
{
return kExpandedCellHeight; //It's not necessary a constant, though
}
else
{
return kNormalCellHeigh; //Again not necessary a constant
}
}
这里的关键是确定是否应该展开/折叠单元格,并在委托方法中返回正确的高度。因此,请相应地设计您的需求。您是否只想在折叠时降低行高?不,我想折叠最后一个项目。单击第二个单元格时,我想一次展开一个单元格。最后一个项目是什么意思?假设您单击了第一个单元格,并且展开了第一个单元格或行。现在我想如果我点击了tableView的第二行或单元格,我想折叠tableView的第一个单元格。这是没有发生的您在您的
didselectrow
中做了什么?您只想在折叠时降低行高度吗?不,我想折叠最后一个项目。当单击第二个单元格时,我想一次展开一个单元格。最后一个项目是什么意思?假设您单击了第一个单元格,并且第一个单元格或行被展开。现在我想如果我点击了tableView的第二行或单元格,我想折叠tableView的第一个单元格。这是没有发生的在didselectrow
中你在做什么?如果你想在单元格选择中这样做,那么在-(void)tableView:(UITableView*)tableView didselectrowatinexpath:(NSIndexPath*)indexPath{//here}中应用逻辑如果你想在单元格选择中这样做,那么在-(void)tableView中应用逻辑:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{//here}