简体   繁体   中英

Resizing the height of UILabel with the height of tableViewCell height

I am new to iOS development. I am using tableView with of dynamic height. The height of tableViewCell increases or decreases on the click for this I am using this code...

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

        }
}

Now I want to add some label to show some information on the tableViewCell but when I click on the cell it resizes perfectly but label on the cell does not resize.Please tell me how can I resize the UILabels with the Cell height. Any help will be appreciated...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM