将tableView单元格扩展和折叠到标签文本

时间:2015-06-24 07:06:45

标签: ios objective-c uitableview

我在 TableViewCell 中有一个标签,其中有多行文字。最初在标签上它只显示一行。我在那个牢房上有一个按钮。我想通过点击按钮直到标签文字的高度来扩展单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"tabCell";
   _cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
   NSManagedObject *device = [self.persons objectAtIndex:indexPath.row];

   UILabel *nameLabel = (UILabel*)[_cell.contentView viewWithTag:2];
  nameLabel.text = [NSString stringWithFormat:@"%@", [device valueForKey:@"name"]];

   UILabel *dateLabel = (UILabel *)[_cell.contentView viewWithTag:3];
   dateLabel.text = [NSString stringWithFormat:@"%@",[device valueForKey:@"date"]];

   UILabel *descLabel = (UILabel *)[_cell.contentView viewWithTag:4];
//descTextView.text = [NSString stringWithFormat:@"%@",[device valueForKey:@"desc"]];


   UIImageView *personImage = (UIImageView *)[_cell.contentView viewWithTag:1];
   UIImage *personImg = [UIImage imageWithData:[device valueForKey:@"image"]];
   personImage.image = personImg;

    UIButton *viewMoreButton = (UIButton *)[_cell.contentView viewWithTag:5];
    [viewMoreButton addTarget:self
             action:@selector(myAction)
   forControlEvents:UIControlEventTouchUpInside];

    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:[device valueForKey:@"desc"]
                                                                 attributes:@{ NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:17]}];


    reqFrame=[attrString boundingRectWithSize:CGSizeMake(descLabel.frame.size.height, CGFLOAT_MAX)options:NSStringDrawingUsesLineFragmentOrigin
                                  context:nil];


    descLabel.attributedText = attrString;




    return _cell;
}

- (void)myAction{

   //what to write here?

}

3 个答案:

答案 0 :(得分:1)

首先,在willDisplayCell中构建单元格不是一个好习惯。请改用heightForRowAtIndexPaththis

其次要做你想做的事,你必须在[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationAutomatic]; 设置所需的高度。完成后,在按钮选择器调用中使用

刷新特定单元格
- (void)buttonSelector
{
    myLabel.text = @"YOUR TEXT";
    [myLabel sizeToFit];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
    return yourLabel.height;
}

实施类似:

{{1}}

答案 1 :(得分:0)

无论如何需要获得引用单元格 indexPath ,在myAction()中点击按钮。

创建一个私有变量CGFloat newCellHeight;并将其初始化为0.0。 让相关单元格引用selectedCell,选择的索引路径为indexPathOfTappedCell

- (void)myAction{
        UILabel *descLabel = (UILabel*)[selectedCell.contentView viewWithTag:4];
        [descLabel sizeToFit]; // Automatically increases the height of the label as required
        newCellHeight = CGRectGetMaxY(descLabel.frame) + 10.0; // Extra padding 10.0
        [tableView reloadRowsAtIndexPaths:@[indexPathOfTappedCell] withRowAnimation:UITableViewRowAnimationAutomatic];
}

现在在视图控制器中添加以下TableView委托方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == indexPathOfTappedCell.section &&
        indexPath.row == indexPathOfTappedCell.row ) {
        return newCellHeight;
    }
    return 44.0; // Suppose default height is 44.0
}

答案 2 :(得分:0)

你需要维护两个变量,只需给你的按钮添加标签,并通过其标签检测其重新加载该特定单元格和另一个BOOL变量以检测按钮是否被触摸。您应该在heightForRowAtIndexPath方法中计算高度。我发布代码,它可能会帮助你

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

if(isReadMoreButtonTouched && [indexPath row]== indexOfReadMoreButton) {
    NSString *yourText = [arr1 objectAtIndex:indexPath.row]; // your text

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIFont fontWithName:@"Avenir Next" size:14], NSFontAttributeName,
                                [UIColor grayColor], NSForegroundColorAttributeName,
                                nil]; // set custom attributes

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:yourText attributes:attributes];

    CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(625, CGFLOAT_MAX)
                                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                        context:nil]; //here 625 is width of label

    NSLog(@"height = %f", paragraphRect.size.height);
    return paragraphRect.size.height;

}
else{

    return 200; //default height taken in storyboard
}
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

cell.lblKB.text = [arr objectAtIndex:indexPath.row];
cell.lblDetail.text = [arr1 objectAtIndex:indexPath.row];

NSString *yourText = [arr1 objectAtIndex:indexPath.row];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                            [UIFont fontWithName:@"Avenir Next" size:14], NSFontAttributeName,
                            GrayColor, NSForegroundColorAttributeName,
                            nil];   //your custom attributes

NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:yourText attributes:attributes];

CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(625, CGFLOAT_MAX)
                                                    options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                    context:nil];

NSLog(@"height = %f", paragraphRect.size.height);

if (paragraphRect.size.height<200) {
    cell.btnMore.hidden = YES;
}
else{
    cell.btnMore.hidden = NO;
}

cell.btnMore.tag = indexPath.row;

return cell;
}


// action of button click

-(IBAction)MoreBtnClicked:(id)sender {

if (!isReadMoreButtonTouched) {
    isReadMoreButtonTouched = YES;
}
else{

    isReadMoreButtonTouched = NO;

}

indexOfReadMoreButton = [sender tag];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];
[self.tblKB reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; //reload that cell of your tableview

}