首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewAutomaticDimension不工作

UITableViewAutomaticDimension不工作
EN

Stack Overflow用户
提问于 2015-01-06 17:52:36
回答 2查看 3.7K关注 0票数 2

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

代码语言:javascript
复制
 - (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:

代码语言:javascript
复制
- (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 Day\nRepeats", 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: method并返回UITableViewAutomaticDimension,但仍然没有成功。

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

下面是该应用程序的ScreenShot:http://manikkalra.com/screen.png http://manikkalra.com/screen2.png

任何帮助都将不胜感激!

EN

回答 2

Stack Overflow用户

发布于 2015-03-05 18:10:47

虽然在我看来,这应该是由tableView reloadData自动调用的,但它不是。

您必须调用:

代码语言:javascript
复制
[YOUR_TABLE_VIEW reloadSections:[NSIndexSet indexSetWithIndex:YOUR_SECTION_INDEX] withRowAnimation:YOUR_UITABLEVIEW_ANIMATION];

之前(这一点很重要)

代码语言:javascript
复制
[YOUR_TABLE_VIEW reloadData]

YOUR_TABLE_VIEW_ANIMATION的选项包括: UITableViewRowAnimationBottom、UITableViewRowAnimationNone、UITableViewRowAnimationTop…

YOUR_SECTION_INDEX是一个整数。

希望这能有所帮助

票数 2
EN

Stack Overflow用户

发布于 2015-09-21 01:53:02

只需调用tableView.layoutSubviews()

代码语言:javascript
复制
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    ///fix bug in xcode UITableViewAutomaticDimension
    tableView.layoutSubviews()
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27796072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档