我通过改变高度来扩展细胞
我想打开更多信息,例如每个单元格选择的小视图,扩展表格视图单元格。
当我们再次点击时,我们选择了细胞必须再次展开和关闭。
我正在使用下面的代码,它工作正常,但如何显示特定单元格位置的隐藏视图。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UILabel *label = nil;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
// cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] ;
}
StudentClass *student = (StudentClass *) [arrayitems objectAtIndex:indexPath.row];
//If this is the selected index then calculate the height of the cell based on the amount of text we have
if(selectedIndex == indexPath.row)
{
UILabel *cellLabeltotaldays12=[[UILabel alloc]initWithFrame:CGRectMake(520, 80, 100, 12)];
cellLabeltotaldays12.font =[ UIFont systemFontOfSize:14];
cellLabeltotaldays12.text=student.totaldays;
[cell addSubview:cellLabeltotaldays12];
}
else {
//Otherwise just return the minimum height for the label.
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//The user is selecting the cell which is currently expanded
//we want to minimize it back
if(selectedIndex == indexPath.row)
{
selectedIndex = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
//First we check if a cell is already expanded.
//If it is we want to minimize make sure it is reloaded to minimize it back
if(selectedIndex >= 0)
{
NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
}
//Finally set the selected index to the new selection and reload it to expand
selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
答案 0 :(得分:3)
您需要根据您的要求更改heightForRow方法的高度。
答案 1 :(得分:1)
试试这个
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [Comments_text objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height+40, 60.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"commentcell";
CommentsCell1 *cell = [Comments_table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CommentsCell1 alloc] initWithFrame:CGRectZero] ;
cell.userInteractionEnabled=YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *text = [Comments_text objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
[cell.Comment_Text setText:text];
[cell.Comment_Text setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN+cell.Comment_Emailimg.frame.origin.y+cell.Comment_Emailimg.frame.size.height, tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), MAX(size.height,20.0f))];
}
return cell;
}