bluetooth99 2013-03-25 02:11 采纳率: 0%
浏览 1042

如果实现点击后增大标签和单元的尺寸

功能要求实现点击单元的seeMoreBtn 之后,单元尺寸会变大。标签和单元的长度不同,但是他们在同一个label中。如果尺寸过大,我就增加了一个see more button (查看更多)。点击之后在下方显示内容。问题是:怎么放大标签和单元的尺寸?

- (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, MAX(size1.height, 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
    }
  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-19 22:44
    关注

    ChatGPT尝试为您解答,仅供参考
    在 increaseSize: 方法中,您可以改变 label 的 frame,并刷新表视图以重新调整单元格的大小。


    例如,您可以将 label 的高度扩大一倍,然后使用表视图的 reloadRowsAtIndexPaths:withRowAnimation: 方法来刷新单元格:

    - (void)increaseSize:(UIButton *)sender {
        // Get the index path for the cell containing the button that was tapped
        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    
        // Get the label for the cell
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        UILabel *label = (UILabel *)[cell viewWithTag:1000];
    
        // Increase the label's height by a factor of 2
        CGRect labelFrame = label.frame;
        labelFrame.size.height *= 2;
        label.frame = labelFrame;
    
        // Reload the cell to adjust its size
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    

    注意:这只是一个简单的示例,实际应用中可能需要更复杂的逻辑。

    评论

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换