Ios 将tableView单元格展开和折叠到标签';s文本

Ios 将tableView单元格展开和折叠到标签';s文本,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我在TableViewCell中有一个标签,其中有多行文本。最初在标签上仅显示一行。我在那个手机上有一个按钮。我想通过单击按钮将单元格扩展到标签文本的最高位 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"tabCell"; _cell = [table

我在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?

}

首先,在
cellforrowatinexpath
中构建单元格不是一个好的做法。改用
willDisplayCell

其次,要执行所需操作,必须在
heightforrowatinexpath
中设置所需的高度。完成后,在按钮选择器调用中使用

[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;
}

无论如何,需要获取单元格的引用和indexath,在
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
}

您需要维护两个变量,只需将标记赋予按钮,并通过其标记在操作中检测以重新加载特定单元格,以及另一个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

}
-(CGFloat)tableView:(UITableView*)tableView rowatinexpath的高度:(nsindepath*)indepath{
如果(isReadMoreButtonTouched&&[indexPath行]==indexOfReadMoreButton){
NSString*yourText=[arr1 objectAtIndex:indexath.row];//您的文本
NSDictionary*属性=[NSDictionary Dictionary WithObjectsAndKeys:
[UIFont fontWithName:@“Avenir Next”大小:14],NSFontAttributeName,
[UIColor grayColor],NSForegroundColorAttributeName,
nil];//设置自定义属性
NSAttributedString*attributedText=[[NSAttributedString alloc]initWithString:yourText属性:属性];
CGRect PARATRAPHRECT=[attributedText BoundingDirectWithSize:CGSizeMake(625,CGFLOAT_MAX)
选项:(NSStringDrawingUserLineFragmentOrigin | NSStringDrawingUserFontLeading)
上下文:nil];//这里625是标签的宽度
NSLog(@“height=%f”,paragraphRect.size.height);
返回段落rect.size.height;
}
否则{
return 200;//脚本中的默认高度
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
cell=[tableView dequeueReusableCellWithIdentifier:@“cell”];
cell.lblKB.text=[arr objectAtIndex:indexath.row];
cell.lblDetail.text=[arr1 objectAtIndex:indexath.row];
NSString*yourText=[arr1 objectAtIndex:indexath.row];
NSDictionary*属性=[NSDictionary Dictionary WithObjectsAndKeys:
[UIFont fontWithName:@“Avenir Next”大小:14],NSFontAttributeName,
灰色,NSForegroundColorAttributeName,
nil];//您的自定义属性
NSAttributedString*attributedText=[[NSAttributedString alloc]initWithString:yourText属性:属性];
CGRect PARATRAPHRECT=[attributedText BoundingDirectWithSize:CGSizeMake(625,CGFLOAT_MAX)
选项:(NSStringDrawingUserLineFragmentOrigin | NSStringDrawingUserFontLeading)
上下文:无];
NSLog(@“height=%f”,paragraphRect.size.height);

if(paragraphRect.size.heights)但是如何扩展到标签文本的高度sir?在heightForRowAtIndexPath中返回标签的高度。为了获得标签,可以将其存储在某个集合中,或者给它一个标签,然后使用[view subViewWithTag:tag]检索它起初,标签仅显示两行文本。但单击按钮后,单元格和标签的高度将展开并显示全文。这是我的要求。@MishaGood。我写的内容肯定会对您有所帮助。在此之前,在按钮选择器中,您可以将全文指定给按钮并调用[标签尺寸合适]使标签设置显示整个文本所需的最小宽度和高度。我们是否可以将此讨论转移到chat@Misha?indexPathOfTappedCell和indexPath.row应该相同。不是吗?@Sauvik?indexPath.row哪一个?索引路径包含节和行。其中indexPath.row仅包含行号。未存储节值。只有一个你只要告诉我按钮的动作应该写些什么就行了?我两天之内就做不成这件事了(@Sauvik DaThe indexPath.section==INDEXPATHOFTCAPEDCELL.section将其更改为indexPath.section==0。无论如何,您是否准备好在按钮点击事件中引用单元格和索引路径?我已经相应地修改了该函数,请让我知道它是否工作。