2

Using tableView estimatedHeightForRowAtIndexPath make cell blinks . I use following code

-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return UITableViewAutomaticDimension;
 }

Create UITableViewCell textLabel vertically align to be always at the top. I use following code but doesnt work.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier1 = @"Cell";
        UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell1 == nil) {
            cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier1];
        }

         cell1.textLabel.text = [self.itemDict objectForKey:kDESCRIPTION];
        cell1.textLabel.numberOfLines = 0;
        cell1.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
        cell1.textLabel.textColor = TextColor;
         [cell1.textLabel sizeToFit];
        return cell1;
}

I have got as First Image but want Second Image

enter image description here

I checked the following link but doesnt work out

Vertically align text to top within a UILabel

enter image description here

Community
  • 1
  • 1
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57

1 Answers1

2

Look at this

Shortly, you can achieve this by to ways - programmatically and via IB

Using Interface Builder

  1. Set up four constraints - top, leading, trainling, height. The height constraint is mandatory.

  2. Then go to the label's attributes inspector and set number of lines to 0

  3. Go to the label's size inspector and increase vertical ContentHuggingPriority and vertical ContentCompressionResistancePriority.

  4. Select and edit height constraint.

  5. And decrease height constraint priority.

EDIT

Add flexible height constraint programmatically:

  [cell1.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell1.textLabel
                                                       attribute:NSLayoutAttributeHeight
                                                       relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                          toItem:nil
                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                      multiplier:1.0
                                                        constant:0.0]];

other constraints:

 NSDictionary *views = @{@"textLabel":cell1.textLabel};

    [cell1.contentView addConstraints:[NSLayoutConstraint
                          constraintsWithVisualFormat:@"H:|[textLabel]|"
                          options:0
                          metrics:nil
                          views:views]];


    [cell1.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell1.textLabel
                                                                  attribute:NSLayoutAttributeTop
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:cell1.contentView
                                                                  attribute:NSLayoutAttributeTop
                                                                 multiplier:1.0
                                                                   constant:10.0]];

Hope this helps

Community
  • 1
  • 1
Doro
  • 2,413
  • 2
  • 14
  • 26