2014-03-11 2 views
1

У меня есть ячейки, которые расширяются, изменяя их высоту.Как увеличить высоту ячейки зависит от содержимого ячейки?

Я хочу открыть дополнительную информацию, такую ​​как небольшой вид каждого выбора ячейки, расширяющая ячейку просмотра таблицы.

Мы выбрали ячейку, которую нужно развернуть и закрыть, когда мы снова щелкнем. enter image description here

Я использую ниже код, он работает нормально, но как показать скрытый вид на конкретную позицию ячейки.

- (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

Попробуйте эти ссылки: http://stackoverflow.com/questions/19855832/expanding-and-collapsing-table-view-cells-in-ios HTTP: //stackoverflow.com/questions/4635338/uitableviewcell-expand-on-click http://stackoverflow.com/questions/15109099/expand-uitableview-cell-in-uitableviewstyleplain –

+0

Эй у вас искать первый в Google. По этой теме есть много решений. как один URL-адрес, который доступен в stackoverflow, точно соответствует вашему взгляду. http://stackoverflow.com/questions/19855832/expanding-and-collapsing-table-view-cells-in-ios – Thukaram

ответ

3

Вам необходимо изменить высоту в методе heightForRow согласно вашему требованию.

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; 

} 
Смежные вопросы