菜鸟教程小白 发表于 2022-12-12 13:33:36

ios - UITableViewAutomaticDimension 不工作


                                            <p><p>我正在以编程方式为日历应用程序制作一个表格 View ,并且需要根据它们持有的文本调整我的表格 View 单元格的大小。我正在尝试使用自动布局,但由于某种原因我无法让它工作</p>

<pre><code> - (void)viewDidLoad
{
    ......
    // Set up the table view
    self.tableView.tableHeaderView = imageView;
    self.tableView.tableHeaderView.backgroundColor = ;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.tableView.estimatedRowHeight = 80.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    [self.tableView registerClass:UITableViewCell.class
         forCellReuseIdentifier:@&#34;cell&#34;];
}
</code></pre>

<p>这是我的 cellForRowAtIndexPath :</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@&#34;cell&#34;
                                                            forIndexPath:indexPath];

    if(cell == nil)
      cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@&#34;cell&#34;];

    cell.detailTextLabel.textColor = ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if(indexPath.row == TableViewRowTitleAndDate)
    {
      cell.textLabel.numberOfLines = 0;
      cell.textLabel.text = self.event.title;
      cell.detailTextLabel.numberOfLines = 0;

      //Get repeating status

      // Set date and time
      NSString *dateString= [NSDateFormatter localizedStringFromDate:self.event.startDate
                                                               dateStyle:NSDateFormatterFullStyle
                                                               timeStyle:NSDateFormatterNoStyle];

      if(self.event.allDay)
      {
            cell.detailTextLabel.text = ;
      }
      else
      {
            NSDateFormatter *formatter = [ init];
            ;
            NSString *startTimeString = ;
            NSString *endTimeString = ;
            cell.detailTextLabel.text = ;
      }
    }
}
</code></pre>

<p>但由于某种原因,我无法让自动布局工作。我还尝试添加 hightForRowAtIndexPath: 方法并返回 UITableViewAutomaticDimension,但仍然没有运气。 </p>

<p>知道我正在尝试将图像设置为使用 SDWebImage 下载的 tableViewHeaderView 也可能会有所帮助。 </p>

<p>这是应用程序的屏幕截图:
<a href="http://manikkalra.com/screen.png" rel="noreferrer noopener nofollow">http://manikkalra.com/screen.png</a>
<a href="http://manikkalra.com/screen2.png" rel="noreferrer noopener nofollow">http://manikkalra.com/screen2.png</a> </p>

<p>任何帮助将不胜感激! </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>虽然我认为这应该由 自动调用(有可能被覆盖),但事实并非如此。所以这里是解决方案。</p>

<p>你必须打电话:</p>

<pre><code> withRowAnimation:YOUR_UITABLEVIEW_ANIMATION];
</code></pre>

<p>Right BEFORE(这很重要)</p>

<pre><code>
</code></pre>

<p>YOUR_TABLE_VIEW_ANIMATION 的选项有:UITableViewRowAnimationBottom、UITableViewRowAnimationNone、UITableViewRowAnimationTop…</p>

<p>YOUR_SECTION_INDEX 是一个整数。</p>

<p>希望对你有帮助</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewAutomaticDimension 不工作,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27796072/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27796072/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewAutomaticDimension 不工作