UITableViewAutomaticDimension 不起作用



我正在以编程方式为日历应用程序制作表格视图,并且需要根据表格视图单元格包含的文本调整它们的大小。我正在尝试使用自动布局,但由于某种原因无法使其工作

 - (void)viewDidLoad
{
    ......
    // Set up the table view
    self.tableView.tableHeaderView = imageView;
    self.tableView.tableHeaderView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 80.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView registerClass:UITableViewCell.class
           forCellReuseIdentifier:@"cell"];
}

这是我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"
                                                            forIndexPath:indexPath];
    if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.detailTextLabel.textColor = [UIColor darkGrayColor];
    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 = [NSString stringWithFormat:@"%@nAll DaynRepeats", dateString];
        }
        else
        {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"h:mm a"];
            NSString *startTimeString = [formatter stringFromDate:self.event.startDate];
            NSString *endTimeString = [formatter stringFromDate:self.event.endDate];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@n%@ - %@nRepeats", dateString, startTimeString, endTimeString];
        }
    }
}

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

此外,知道我正在尝试将图像设置为使用 SDWebImage 下载的 tableViewHeaderView 可能会有所帮助。

这是应用程序的屏幕截图:http://manikkalra.com/screen.pnghttp://manikkalra.com/screen2.png

任何帮助将不胜感激!

虽然在我看来这应该由 [tableView reloadData] 自动调用(有可能被覆盖),但事实并非如此。所以这是解决方案。

您必须致电:

[YOUR_TABLE_VIEW reloadSections:[NSIndexSet indexSetWithIndex:YOUR_SECTION_INDEX] withRowAnimation:YOUR_UITABLEVIEW_ANIMATION];

就在之前(这很重要)

[YOUR_TABLE_VIEW reloadData]

YOUR_TABLE_VIEW_ANIMATION的选项是:UITableViewRowAnimationBottom,UITableViewRowAnimationNone,UITableViewRowAnimationTop...

YOUR_SECTION_INDEX是一个整数。

希望这有帮助

只需调用tableView.layoutSubviews()

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    ///fix bug in xcode UITableViewAutomaticDimension
    tableView.layoutSubviews()
}

最新更新