0

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

4

1 回答 1

0

Use the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to achieve this. It is also good to move the whole logic for changing the height for the cells from didSelectRow to cellForRowAtIndexPath.

To answer your question:

- (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;
}

where textLabel is the name of your label. The basic idea is that you take the frame of the label, then set it's height to the height of cell's view and then set the new size to the label.

于 2013-10-03T12:33:54.663 回答