ios – 如何在单击TableViewCell中的按钮时增加标签和单元格大小.

我想在单元格上单击seeMoreBtn后扩展单元格大小.

标签和单元格具有不同的长度,但它们是标签大小的约束.

当状态太大时,我添加一个seeMoreBtn,点击后查看更多剩余文本将在下面显示,然后如何增加标签和单元格大小.

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


        Nsstring *text = [items objectAtIndex:[indexPath row]];
        CGSize constraint = CGSizeMake(300.0f,150.0f);
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByCharWrapping];

        CGFloat height1 = MAX(size.height,44.0f);
        return height1 + (40.0f);
        }
     - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Nsstring *CellIdentifier = [Nsstring stringWithFormat:@"Cell-%d",indexPath.row];

        cell=[tv dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        int lbltag = 1000;

        label=[[UILabel alloc]initWithFrame:CGRectZero];
        [label setLineBreakMode:NSLineBreakByWordWrapping];
        [label setMinimumScaleFactor:14.0f];
        [label setNumberOfLines:0];
        [label setFont:[UIFont systemFontOfSize:14.0f]];
        Nsstring *text = [items objectAtIndex:[indexPath row]];
        [label setText:text];
        label.tag = lbltag;
        [cell addSubview:label];

        CGSize constraint1=CGSizeMake(300.0f,150.0f);
        CGSize size1=[text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];


        [label setFrame:CGRectMake(10.0f,10.0f,300.0f,MAX(size1.height,44.0f))];

        int countText=text.length;

        if (countText>=350) {
            seeMoreBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [seeMoreBtn setTitle:@"See More" forState:UIControlStatenormal];
            seeMoreBtn.frame=CGRectMake(220.0f,44.0f)-10,80.0f,20.0f);
            seeMoreBtn.tag=indexPath.row ;
              [seeMoreBtn addTarget:self action:@selector(increaseSize:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:seeMoreBtn];            
        }
        return cell;
    }

    -(void)increaseSize:(UIButton *)sender{
//What to write here that can adjust the label and cell size

    }

解决方法

如果您继承UITableViewCell并使用layoutSubviews调整单元格的大小,那会更好.
//In SMTableViewCell.h

@interface SMTableViewCell : UITableViewCell

@property (weak,nonatomic) IBOutlet UILabel *statusLabel;
@property (weak,nonatomic) IBOutlet UIButton *seeMoreButton;

//SMTableViewCell.m

- (void)layoutSubviews
{
    CGRect labelFrame = self.statusLabel.frame;
    labelFrame.size.height = self.frame.size.height - 55.0f;
    self.statusLabel.frame = labelFrame;

    CGRect buttonFrame = self.seeMoreButton.frame;
    buttonFrame.origin.y = labelFrame.origin.y+labelFrame.size.height+10.0f;
    self.seeMoreButton.frame = buttonFrame;
}

保留一个数组来存储selectedindexPaths:

@property (nonatomic,strong) NSMutableArray *selectedindexPaths;

计算单元格的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    BOOL isSelected = [self.selectedindexPaths containsObject:indexPath];

    CGFloat maxHeight = MAXFLOAT;
    CGFloat minHeight = 40.0f;

    CGFloat constrainHeight = isSelected?maxHeight:minHeight;
    CGFloat constrainWidth  = tableView.frame.size.width - 20.0f;

    Nsstring *text       = self.items[indexPath.row];
    CGSize constrainSize = CGSizeMake(constrainWidth,constrainHeight);
    CGSize labelSize     = [text sizeWithFont:[UIFont systemFontOfSize:15.0f]
                            constrainedToSize:constrainSize
                                lineBreakMode:NSLineBreakByCharWrapping];

    return MAX(labelSize.height+75,100.0f);

}

初始化自定义显示更多TableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static Nsstring *cellIdentifier = @"CellIdentifier";

    SMTableViewCell *cell= (SMTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:NsstringFromClass([SMTableViewCell class])
                                             owner:nil
                                           options:nil] lastObject];
    }

    BOOL isSelected = [self.selectedindexPaths containsObject:indexPath];
    cell.statusLabel.numberOfLines = isSelected?0:2;

    Nsstring *text = self.items[indexPath.row];
    cell.statusLabel.text = text;


    Nsstring *buttonTitle = isSelected?@"See Less":@"See More";
    [cell.seeMoreButton setTitle:buttonTitle forState:UIControlStatenormal];
    [cell.seeMoreButton addTarget:self action:@selector(seeMoreButtonpressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.seeMoreButton setTag:indexPath.row];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

按钮点击事件方法

- (void)seeMoreButtonpressed:(UIButton *)button
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
    [self addOrRemoveSelectedindexPath:indexPath];
}

- (void)addOrRemoveSelectedindexPath:(NSIndexPath *)indexPath
{
    if (!self.selectedindexPaths) {
        self.selectedindexPaths = [NSMutableArray new];
    }

    BOOL containsIndexPath = [self.selectedindexPaths containsObject:indexPath];

    if (containsIndexPath) {
        [self.selectedindexPaths removeObject:indexPath];
    }else{
        [self.selectedindexPaths addobject:indexPath];
    }

    [self.tableView reloadRowsAtIndexPaths:@[indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];

}

如果选择了单元格,则会给出相同的事件:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    [self addOrRemoveSelectedindexPath:indexPath];

}

样品Demo project link.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...