<tfoot id='BCQhw'></tfoot>
        • <bdo id='BCQhw'></bdo><ul id='BCQhw'></ul>
      1. <small id='BCQhw'></small><noframes id='BCQhw'>

        <legend id='BCQhw'><style id='BCQhw'><dir id='BCQhw'><q id='BCQhw'></q></dir></style></legend>

        <i id='BCQhw'><tr id='BCQhw'><dt id='BCQhw'><q id='BCQhw'><span id='BCQhw'><b id='BCQhw'><form id='BCQhw'><ins id='BCQhw'></ins><ul id='BCQhw'></ul><sub id='BCQhw'></sub></form><legend id='BCQhw'></legend><bdo id='BCQhw'><pre id='BCQhw'><center id='BCQhw'></center></pre></bdo></b><th id='BCQhw'></th></span></q></dt></tr></i><div id='BCQhw'><tfoot id='BCQhw'></tfoot><dl id='BCQhw'><fieldset id='BCQhw'></fieldset></dl></div>

        如何使用 AutoLayout 以编程方式创建自定义 UITableViewCell

        时间:2023-09-12
        <i id='xAJ4C'><tr id='xAJ4C'><dt id='xAJ4C'><q id='xAJ4C'><span id='xAJ4C'><b id='xAJ4C'><form id='xAJ4C'><ins id='xAJ4C'></ins><ul id='xAJ4C'></ul><sub id='xAJ4C'></sub></form><legend id='xAJ4C'></legend><bdo id='xAJ4C'><pre id='xAJ4C'><center id='xAJ4C'></center></pre></bdo></b><th id='xAJ4C'></th></span></q></dt></tr></i><div id='xAJ4C'><tfoot id='xAJ4C'></tfoot><dl id='xAJ4C'><fieldset id='xAJ4C'></fieldset></dl></div>

        <tfoot id='xAJ4C'></tfoot>

              <small id='xAJ4C'></small><noframes id='xAJ4C'>

              • <bdo id='xAJ4C'></bdo><ul id='xAJ4C'></ul>
              • <legend id='xAJ4C'><style id='xAJ4C'><dir id='xAJ4C'><q id='xAJ4C'></q></dir></style></legend>
                    <tbody id='xAJ4C'></tbody>
                  本文介绍了如何使用 AutoLayout 以编程方式创建自定义 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试实现一个 UITableView,它的行为类似于 twitter 客户端的时间线.现在我只是试图在 UITableViewCell 中获取两个标签.正如 this Stack Overflow answer 所推荐的,我为每个布局使用了不同的重用标识符.我的布局很简单,由一个标签或两个标签组成.最终我将调整 UITableViewCells 的高度,但首先我需要让单元格填充内容.

                  I'm trying to implement a UITableView that will behave similarly to the timeline of a twitter client. Right now I'm simply attempting to get two labels inside a UITableViewCell. As recommended by this Stack Overflow answer, I'm using a different reuseIdentifier for each layouts. My layouts are simple, consisting of either a single label or two labels. Eventually I will be adjusting the height of the UITableViewCells but first I need to get the cells populated with content.

                  如果我使用 initWithFrame: 设置它们的框架,我可以获得标签,因此显示出来,但是没有实现约束.

                  I can get the labels so show up if I set their frame with initWithFrame:, however the constraints aren't being implemented.

                  • 问题:是什么阻止了标签和约束的出现?我在 UITableViewCell 的实现中显然遗漏了一些东西,但我不知道它是什么.

                  • Question: What is preventing the labels and constraints from appearing? I'm clearly missing something in my implementation of the UITableViewCell but I have no idea what it is.

                  第二个问题:我是否为 viewDidLoad 中的每个重用标识符正确注册了 UITableViewCell 类?

                  Secondary question: Am I registering the UITableViewCell class correctly for each reuseIdentifier in viewDidLoad?

                  这可能会让人觉得很困难,但 Interface Builder 让我很困惑,我想在代码中完成这一切.

                  This might come across as being difficult but Interface Builder confuses me, I would like to accomplish this all in code.

                  下面是名为 TVTCell.h 的自定义 UITableViewCell 的代码:

                  Here is the code for the custom UITableViewCell named TVTCell.h:

                  static NSString * const kCellIDTitle = @"CellWithTitle";
                  static NSString * const kCellIDTitleMain = @"CellWithTitleMain";
                  
                  @interface TVTCell : UITableViewCell
                  {
                      NSString *reuseID;
                  }
                  
                  @property (nonatomic, strong) UILabel *nameLabel;
                  @property (nonatomic, strong) UILabel *mainLabel;
                  
                  @end
                  

                  还有 TVTCell.m:

                  And TVTCell.m:

                  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
                  {
                      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
                      if (self) {
                          reuseID = reuseIdentifier;
                  
                          nameLabel = [[UILabel alloc] init];
                          [nameLabel setTextColor:[UIColor blackColor]];
                          [nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
                          [nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
                          [nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
                          [self.contentView addSubview:nameLabel];
                  
                          mainLabel = [[UILabel alloc] init];
                          [mainLabel setTextColor:[UIColor blackColor]];
                          [mainLabel setBackgroundColor:[UIColor colorWithHue:66 saturation:100 brightness:63 alpha:1]];
                          [mainLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
                          [mainLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
                          [self.contentView addSubview:mainLabel];
                  
                          [self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
                  
                      }
                      return self;
                  }
                  
                  
                  - (void)updateConstraints
                  {
                      [super updateConstraints];
                  
                      NSDictionary *views = NSDictionaryOfVariableBindings(nameLabel, mainLabel);
                      if (reuseID == kCellIDTitle) {
                          NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
                                                                  options: NSLayoutFormatAlignAllCenterX
                                                                  metrics:nil
                                                                    views:views];
                          [self.contentView addConstraints:constraints];
                          constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel]|"
                                                                                options: NSLayoutFormatAlignAllCenterX
                                                                                metrics:nil
                                                                                  views:views];
                          [self.contentView addConstraints:constraints];
                      }
                      if (reuseID == kCellIDTitleMain) {
                          NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
                                                                                         options: NSLayoutFormatAlignAllCenterX
                                                                                         metrics:nil
                                                                                           views:views];
                          [self.contentView addConstraints:constraints];
                  
                          constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainLabel]|"
                                                                                         options: NSLayoutFormatAlignAllCenterX
                                                                                         metrics:nil
                                                                                           views:views];
                          [self.contentView addConstraints:constraints];
                  
                          constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel][mainLabel]|"
                                                                                options: NSLayoutFormatAlignAllLeft
                                                                                metrics:nil
                                                                                  views:views];
                          [self.contentView addConstraints:constraints];
                  
                          [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:nameLabel
                                                       attribute:NSLayoutAttributeHeight
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:nil
                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                      multiplier:0.0
                                                        constant:44.0]];
                          [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:nameLabel
                                                                                       attribute:NSLayoutAttributeWidth
                                                                                       relatedBy:NSLayoutRelationEqual
                                                                                          toItem:self.contentView
                                                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                                                      multiplier:0.0
                                                                                        constant:1]];
                      }
                  }
                  

                  抱歉,代码太多了.这是我的 UITableView 的 tableView:cellForRowAtIndexPath:

                  Sorry, ton of code. Here's my UITableView's tableView:cellForRowAtIndexPath:

                  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                  {
                      if (indexPath.row == 0 || indexPath.row == 2 || indexPath.row == 3) {
                          TVTCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIDTitle forIndexPath:indexPath];
                  
                          [[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]];
                  
                          return cell;
                      } else if (indexPath.row == 1 || indexPath.row == 4 || indexPath.row == 5) {
                          TVTCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIDTitleMain forIndexPath:indexPath];
                  
                          [[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]];
                          [[cell mainLabel] setText:[dataArray objectAtIndex:indexPath.row]];
                  
                          return cell;
                      } else
                      {
                          UITableViewCell *badCell = [[UITableViewCell alloc] init];
                          NSLog(@"Warning! returning a cell that shouldnt be here");
                          badCell.textLabel.text = @"Warning!";
                          return badCell;
                      }
                  }
                  

                  最后,UITableView 的 viewDidLoad 方法:

                  And lastly, the UITableView's viewDidLoad method:

                  - (void)viewDidLoad
                  {
                      [super viewDidLoad];
                  
                      [[self tableView] registerClass:[TVTCell class] forCellReuseIdentifier:kCellIDTitle];
                      [[self tableView] registerClass:[TVTCell class] forCellReuseIdentifier:kCellIDTitleMain];
                  }
                  

                  推荐答案

                  你的代码有几个问题.首先,我想你会发现,如果你做一些记录,updateConstraints 永远不会被调用.我会将所有代码放在 init 方法中.此外,您的约束中有几处错误.不需要将高度设置为 44 的约束,因为您已经将标签固定到单元格的到和底部.我不知道你想用最后一个做什么,看起来这会使 nameLabel 宽 1 点.此外,您不应该将内容视图的 translatesAutoresizingMaskIntoConstraints 设置为 NO,这会导致奇怪的效果.所以这是我认为你想要的代码:

                  There are several things wrong with your code. First, I think you'll find, if you do some logging, that updateConstraints is never called. I would put all the code in the init method. Also, there are several things wrong in your constraints. The constraint where you set the height to 44 is not needed since you already have the labels pinned to the to and bottom of the cell. I don't know what you're trying to do with that last one, it looks like that would make the nameLabel 1 point wide. Also, you shouldn't set the translatesAutoresizingMaskIntoConstraints to NO for the content view, that causes weird effects. So this is the code I think you want:

                  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
                      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
                      if (self) {
                          reuseID = reuseIdentifier;
                  
                          nameLabel = [[UILabel alloc] init];
                          [nameLabel setTextColor:[UIColor blackColor]];
                          [nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
                          [nameLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
                          [nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
                          [self.contentView addSubview:nameLabel];
                  
                          mainLabel = [[UILabel alloc] init];
                          [mainLabel setTextColor:[UIColor blackColor]];
                          [mainLabel setBackgroundColor:[UIColor colorWithHue:66 saturation:100 brightness:63 alpha:1]];
                          [mainLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:18.0f]];
                          [mainLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
                          [self.contentView addSubview:mainLabel];
                  
                          NSDictionary *views = NSDictionaryOfVariableBindings(nameLabel, mainLabel);
                          if (reuseID == kCellIDTitle) {
                              NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
                                                                                             options: 0
                                                                                             metrics:nil
                                                                                               views:views];
                              [self.contentView addConstraints:constraints];
                              constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel]|"
                                                                                    options: 0
                                                                                    metrics:nil
                                                                                      views:views];
                              [self.contentView addConstraints:constraints];
                          }
                          if (reuseID == kCellIDTitleMain) {
                              NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[nameLabel]|"
                                                                                             options:0
                                                                                             metrics:nil
                                                                                               views:views];
                              [self.contentView addConstraints:constraints];
                  
                              constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mainLabel]|"
                                                                                    options: 0
                                                                                    metrics:nil
                                                                                      views:views];
                              [self.contentView addConstraints:constraints];
                  
                              constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[nameLabel][mainLabel(==nameLabel)]|"
                                                                                    options: 0
                                                                                    metrics:nil
                                                                                      views:views];
                              [self.contentView addConstraints:constraints];
                  
                          }
                      }
                      return self;
                  }
                  

                  这篇关于如何使用 AutoLayout 以编程方式创建自定义 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:UIStackView“无法同时满足约束"在“压扁"上隐藏视图 下一篇:UIScrollView 分页自动布局 &amp;故事板

                  相关文章

                    1. <small id='hCOGr'></small><noframes id='hCOGr'>

                      <i id='hCOGr'><tr id='hCOGr'><dt id='hCOGr'><q id='hCOGr'><span id='hCOGr'><b id='hCOGr'><form id='hCOGr'><ins id='hCOGr'></ins><ul id='hCOGr'></ul><sub id='hCOGr'></sub></form><legend id='hCOGr'></legend><bdo id='hCOGr'><pre id='hCOGr'><center id='hCOGr'></center></pre></bdo></b><th id='hCOGr'></th></span></q></dt></tr></i><div id='hCOGr'><tfoot id='hCOGr'></tfoot><dl id='hCOGr'><fieldset id='hCOGr'></fieldset></dl></div>
                      <tfoot id='hCOGr'></tfoot>
                      • <bdo id='hCOGr'></bdo><ul id='hCOGr'></ul>
                      <legend id='hCOGr'><style id='hCOGr'><dir id='hCOGr'><q id='hCOGr'></q></dir></style></legend>