【问题标题】:UITableView row height not changingUITableView 行高不变
【发布时间】:2017-12-22 04:42:50
【问题描述】:

我创建了一个自定义单元格。我有一系列字典。对于字典值,我需要创建UILables。每个单元格可能包含不同数量的UILabels。因此,在我的自定义 UITableViewCell 课程中,我已经这样做了。

- (void)generateCell {


BOOL isLandscape = UIInterfaceOrientationIsLandscape(vc.interfaceOrientation);
for (int i = 0; i < [orderMap count]; i++) {
    UILabel *lbl = [[UILabel alloc] init];
    [lbl setTextColor:[UIColor blueColor]];
    [lbl setBackgroundColor:[UIColor yellowColor]];
    lbl.tag = [[orderMap objectAtIndex:i] intValue];
    [lbl setNumberOfLines:0];
    [self.contentView addSubview:lbl];
    [lbl setTranslatesAutoresizingMaskIntoConstraints:false];

}

if (isLandscape) {

}
else {
    [self setConstraintsForPortrait];
}
}

- (void)setConstraintsForPortrait {


double currentUsedWidth = 0;
double relativeWidthLimit = 1;
int reservedPaddingSpace = 20;
int usableScreenWidthPts = 0;
int xPositionStartingPoint = 15;
int xPosition = xPositionStartingPoint;
int yPosition = INDIVIDUAL_PADDING_PTS;
int fieldCounter = 0;
UIView *previousView;

for (UIView *vw in [self.contentView subviews]) {
    NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:(xPosition + INDIVIDUAL_PADDING_PTS)];
    NSLayoutConstraint *top;

    if (previousView == nil) {
        top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
    }
    else {
        top = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
    }

    NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];
    if (fieldCounter >= [orderMap count]) {
        NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:vw attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:INDIVIDUAL_PADDING_PTS];

        [self.contentView addConstraint:bottom];
    }



    [self.contentView addConstraint:leading];
    [self.contentView addConstraint:top];
    [self.contentView addConstraint:trailing];
    previousView = vw;
}

}

在我的UIViewController viewDidLoad 方法中我已经做到了。

UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:[NSBundle mainBundle]];
[self.tbl registerNib:nib forCellReuseIdentifier:metadataListTableIdentifier];
self.tbl.estimatedRowHeight = 200;
self.tbl.rowHeight = UITableViewAutomaticDimension;
[self.tbl reloadData];

为什么单元格高度没有变化?请帮我。 谢谢

【问题讨论】:

    标签: ios objective-c uitableview nslayoutconstraint


    【解决方案1】:

    设置好tableview的delegate和datasources后,试试这个

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension //return height size whichever you want
        }
    

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200 //return height size whichever you want
    }
    

    【讨论】:

    • 我已经在 ViewDidload 中设置了
    • 你仍然可以尝试这样设置,因为它没有按照你设置的方式工作
    • 我评论了这两行并添加了这些代表。但是没有效果。还是一样的:(
    • 那么你需要检查约束是否不合适并且优先。如果约束是正确的,那么这段代码就可以工作
    • 并检查您在 xib 或 stroyboard 中设置的表格视图高度的高度,它应该与您要设置的高度相同 @myStack
    【解决方案2】:

    按照以下代码:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         return [self getMessageSize:[[_arrCaseActivityList objectAtIndex:indexPath.row] objectForKey:@"message"] Width:275];
    }
    
    -(float )getMessageSize :(NSString *)text Width :(float)textWidth
    {
        NSAttributedString *attributedText =
    [[NSAttributedString alloc]
     initWithString:text
     attributes:@
     {
     NSFontAttributeName: [UIFont fontWithName:@"Rubik-Regular" size:13.0]
     }];
    
        CGRect rect = [attributedText boundingRectWithSize:(CGSize){textWidth, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
        CGSize finalSize = rect.size;
        return finalSize.height+75;
    
    }
    

    【讨论】:

    • 但是如果我设置了约束,单元格大小应该相应地改变不是吗?
    • 删除自定义 Cell 约束并尝试一下。它正在工作。
    【解决方案3】:

    在 Interface Builder 中提供适当的约束,例如。 leading, trailing 等并将 UILabel 的 no of lines 设置为 0。在viewDidLoad方法中添加以下代码。

    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 300 // estimated row height for performance improvement
    

    【讨论】:

      【解决方案4】:

      单击 + 拖动您的 tableview 并设置 viewcontroller 视图等宽和等高

      #pragma mark tableview delegate
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
          return [DATA count];
      }
      
      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      
          PartyListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
          if (cell == nil) {
              // Load the top-level objects from the custom cell XIB.
              NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PartyListCell" owner:self options:nil];
              cell = [topLevelObjects objectAtIndex:0];
          }
      
        cell.textLabel.text = [DATA objectAtIndex:indexPath.row];
      
          return cell;
      }
      
      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
          return 38.0;
      }
      
      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
         }
      
      -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
          UIView *tableviewHeader = [[[NSBundle mainBundle] loadNibNamed:@"PartyListHeaderView" owner:self options:nil] objectAtIndex:0];
          return tableviewHeader;
      }
      
      -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
          return 38.0;
      }
      

      单击按钮加载您的 nib 文件

       if (popUpView== nil) {
              popUpView = [[[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil] objectAtIndex:0];
              [self.view addSubview:popUpView];
              [popUpView setDelegate:self];
              [popUpView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
          }
      

      【讨论】:

      • 这里 PartyListCell 是我的自定义单元格现在试试这个代码
      • 我想动态添加UILabels。并且不同的单元格可能包含不同数量的标签。所以我不能直接使用 cell.labelname 。我正在使用 for 循环添加这些标签
      • 您不需要循环,只需为数据创建一个可变数组并像这样为它们赋予单元格值
      • 即使用户添加后也可以添加新的 UILabel,tbl 应重新加载并添加新标签。如果我不使用循环,我怎么能做到这一点?
      【解决方案5】:

      就我而言,我使用的是函数:

      func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
              return 100
          }
      

      而不是

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
              return 100
          }
      

      确保您使用的是正确的函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-10
        • 2012-10-11
        • 2012-12-22
        • 1970-01-01
        • 2015-04-14
        • 1970-01-01
        • 2014-09-30
        相关资源
        最近更新 更多