dynamically change height of cells in objective C

前端 未结 3 837
你的背包
你的背包 2021-01-06 04:03

I have created an app with a table view. Which uses a view and a label in each cell. But if I create the views and cells in the (!cell) code it returns empty cells and if I

相关标签:
3条回答
  • 2021-01-06 04:03

    This is a part of my code which i used in my app. It works for me fine.Ping me if u need help.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
    CGSize constraintSize = {230.0, 20000}; //230 is cell width & 20000 is max height for cell
    CGSize neededSize = [ [NSString stringWithFormat:@"%@",[cellar objectAtIndex:indexPath.row]] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f] constrainedToSize:constraintSize  lineBreakMode:UILineBreakModeCharacterWrap];
    
    
    return MAX(45, neededSize.height +33);
    
    
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
    
    }
    
    
    CGSize constraintSize = {230.0, 20000};
    
    
    UILabel* label = [[UILabel alloc] init];
    
    [label setNumberOfLines:0];
    
    label.backgroundColor = [UIColor clearColor];
    
    [label setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f]];
    
    label.adjustsFontSizeToFitWidth = NO;
    CGSize neededSize = [ [NSString stringWithFormat:@"%@",[cellar objectAtIndex:indexPath.row] ] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f] constrainedToSize:constraintSize  lineBreakMode:UILineBreakModeCharacterWrap];
    // NSLog(@"Height%f",neededSize.height);
    //NSLog(@"width%f",neededSize.width);
    [label setText:[NSString stringWithFormat:@"%@",[cellar objectAtIndex:indexPath.row] ]];
    
    [label setFrame:CGRectMake(10, 2, 230, MAX(neededSize.height+30, 44.0f))];
    
    [[cell contentView] addSubview:label];
    
    
    
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    
    return cell;
    
    
    }
    

    Hope it helps.!!!

    0 讨论(0)
  • 2021-01-06 04:07

    I saw a lot of solutions but all was wrong or uncomplet. You can solve all problems with 5 lines in viewDidLoad and autolayout. This for objetive C:

    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView setNeedsLayout];
    [self.tableView layoutIfNeeded];
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
    

    For swift 2.0:

     self.tableView.estimatedRowHeight = 80
     self.tableView.rowHeight = UITableViewAutomaticDimension      
     self.tableView.setNeedsLayout()
     self.tableView.layoutIfNeeded()
     self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
    

    Now create your cell with xib or into tableview in your Storyboard With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"

    You can donwload the code in the next url: https://github.com/jposes22/exampleTableCellCustomHeight

    References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

    0 讨论(0)
  • 2021-01-06 04:11
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         return "your content height";
    }
    
    0 讨论(0)
提交回复
热议问题