【问题标题】:Dynamic UITableViewCell per UILable height with landscape portrait orientation support每个 UILable 高度的动态 UITableViewCell,支持横向纵向方向
【发布时间】:2013-09-18 21:02:45
【问题描述】:

我需要根据标签的内容创建一个单元格高度并支持方向,目前我已经尝试过这个并且在纵向上工作得很好,但在横向上却不行,所以需要两种都适用的解决方案,所以如果有人可以帮助我。

提前感谢您的努力。

这是我在 tableview 中的代码

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [arrData objectAtIndex:tableView.tag];    
    NSString *text = [arr objectAtIndex:indexPath.row];    
    CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);    
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];    
    CGFloat height = MAX(size.height, 44.0f);    
    return height + (CELL_CONTENT_MARGIN * 2);
}

// 自定义表格视图单元格的外观。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    UILabel *label = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.accessoryView = nil;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        label = [[UILabel alloc] initWithFrame:CGRectZero];
        [label setLineBreakMode:NSLineBreakByWordWrapping];
        [label setNumberOfLines:0];
        [label setFont:[UIFont systemFontOfSize:20]];
        [label setTag:1];
        [label setBackgroundColor:[UIColor redColor]];                
        [[cell contentView] addSubview:label];
    }

    NSArray *arr = [arrData objectAtIndex:tableView.tag];
    NSString *text = [arr objectAtIndex:indexPath.row];

    CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    if (!label)
        label = (UILabel*)[cell viewWithTag:1];

    [label setText:text];
    [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, contentWidth - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];

    return cell;
}

【问题讨论】:

    标签: iphone uitableview uilabel


    【解决方案1】:

    heightForRowAtIndexPath 中,您可以根据方向设置高度。 当方向改变时,重新加载单元格。在heightForRowAtIndexPath 中使用此值检查方向,而此方法称为-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 重新加载您的单元格。

    所以你的heightForRowAtIndexPath 会是这样的

     if(UIInterfaceOrientationIsPortrait(orientation)){
     }
     else
     {
     }
    

    确保您的单元格子视图保持灵活的自动调整大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多