UITableViewCell textLabel使文本垂直对齐为top& estimatedHeightForRowAtIndexPathUITableViewCell跳转到选定的单元格

时间:2015-07-03 06:00:20

标签: ios objective-c uitableview

使用tableView estimatedHeightForRowAtIndexPath使单元格闪烁。我使用以下代码

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


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

创建UITableViewCell textLabel垂直对齐以始终位于顶部。我使用以下代码但不起作用。

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

我有第一张图片但想要第二张图片

enter image description here

我检查了以下链接,但没有解决

Vertically align text to top within a UILabel

enter image description here

1 个答案:

答案 0 :(得分:1)

查看this

很快,您可以通过以下方式实现这一目标 - 以编程方式并通过IB

使用Interface Builder

  1. 设置四个约束 - 顶部,领先,训练,高度。高度约束是强制性的。

  2. 然后转到标签的属性检查器并将行数设置为0

  3. 转到标签尺寸检查器并增加垂直ContentHuggingPriority和垂直ContentCompressionResistancePriority。

  4. 选择并编辑高度约束。

  5. 并降低高度约束优先级。

  6. 修改

    以编程方式添加灵活的高度约束:

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

    其他限制:

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

    希望这有帮助