• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UITableViewAutomaticDimension 不工作

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:33:36 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

 - (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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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: 方法并返回 UITableViewAutomaticDimension,但仍然没有运气。

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

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

任何帮助将不胜感激!



Best Answer-推荐答案


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

你必须打电话:

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

Right BEFORE(这很重要)

[YOUR_TABLE_VIEW reloadData]

YOUR_TABLE_VIEW_ANIMATION 的选项有:UITableViewRowAnimationBottom、UITableViewRowAnimationNone、UITableViewRowAnimationTop…

YOUR_SECTION_INDEX 是一个整数。

希望对你有帮助

关于ios - UITableViewAutomaticDimension 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27796072/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap