【问题标题】:Resizing the height of UILabel with the height of tableViewCell height使用 tableViewCell 高度调整 UILabel 的高度
【发布时间】:2013-10-03 12:20:31
【问题描述】:

我是 iOS 开发的新手。我正在使用动态高度的 tableView。 tableViewCell 的高度在点击时增加或减少我正在使用此代码...

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.selectedPath isEqual:indexPath])
    {
        return 250;
    }
    else
    {
        return 44;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
onSelectCount++;

    self.selectedPath = indexPath;
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationLeft];
    self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row];
    [self.tableView1 deselectRowAtIndexPath:indexPath animated:YES];

    //First we check if a cell is already expanded.
    //If it is we want to minimize make sure it is reloaded to minimize it back
    if( onSelectCount==1 )
    {
        NSLog(@"num=%d",onSelectCount);
        NSLog(@"First Condition");
        [tableView beginUpdates];
        NSIndexPath *previousPath = [NSIndexPath indexPathForRow:self.selectedRowIndex.integerValue inSection:0];
        self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row];
        self.selectedPath=indexPath;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
    }

    if(self.selectedPath.row!=indexPath.row)
    {
        [tableView beginUpdates];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:selectedPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];

        selectedPath=indexPath;
        onSelectCount=0;
        [self tableView:tableView didSelectRowAtIndexPath:selectedPath];
    }

    if(self.selectedRowIndex.integerValue == indexPath.row && onSelectCount==2)
    {
        [tableView beginUpdates];
        self.selectedRowIndex = [NSNumber numberWithInteger:-1];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        onSelectCount=0;
        [tableView endUpdates];

        }
}

现在我想添加一些标签以在 tableViewCell 上显示一些信息,但是当我单击单元格时,它会完美调整大小,但单元格上的标签不会调整大小。请告诉我如何使用单元格高度调整 UILabel 的大小。任何帮助将不胜感激...

【问题讨论】:

    标签: iphone ios objective-c uitableview


    【解决方案1】:

    使用方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 来实现这一点。将用于更改单元格高度的整个逻辑从 didSelectRow 移动到 cellForRowAtIndexPath 也很好。

    回答你的问题:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
        CGRect sizeRect = cell.textLabel.frame;
        sizeRect.size.height = cell.contentView.frame.size.height;
        cell.textLabel.frame = sizeRect;
    
        return cell;
    }
    

    其中 textLabel 是标签的名称。 基本思想是你取标签的框架,然后将它的高度设置为单元格视图的高度,然后将新的大小设置为标签。

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 2010-10-01
      • 2014-09-30
      • 1970-01-01
      • 2010-11-03
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      相关资源
      最近更新 更多