使用uitableViewCell高度的高度调整UILabel的高度

时间:2013-10-03 12:20:32

标签: iphone ios objective-c uitableview

我是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的大小。任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:0)

使用方法- (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是标签的名称。 基本的想法是你取标签的框架,然后将它的高度设置为单元格视图的高度,然后将新大小设置为标签。

相关问题