草庐IT

ios - 表格 View 中单元格的动态高度不起作用

coder 2024-01-18 原文

我创建了一个 table view,它有 6 个标签和 2 个按钮。我正在使用自动版式。我正在从服务器获取数据。

ViewController.cs

public partial class CaseHistoryController : UIViewController
{
    private List<CaseSearchItem> caseSearchItems;
    CaseHistorySourceClass caseSearchSourceclass;

    public CaseHistoryController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        this.Title = "Case History";

        View.BackgroundColor = UIColor.Clear.FromHexString("#DDDDDD", 1.0f);
        var task = GetCaseHistoryData();
    }

    private async Task GetCaseHistoryData()
    {
        caseSearchItems = await CaseHistoryServices.GetListCaseHistory();
        caseSearchSourceclass = new CaseHistorySourceClass(caseSearchItems);

        CaseHistorySourceClass.RowClicked += (sender, e) =>
        {
            var webController = Storyboard.InstantiateViewController("UserInfoViewController") as UserInfoViewController;
            webController.caseSearchItem = (CaseSearchItem)sender;
            NavigationController.PushViewController(webController, true);
        };
        caseHistoryTableView.Source = caseSearchSourceclass;

        caseHistoryTableView.BackgroundColor = UIColor.Clear.FromHexString("#DDDDDD", 1.0f);
        caseHistoryTableView.SectionHeaderHeight = 0.0f;
        caseHistoryTableView.SectionFooterHeight = 5.0f;
        caseHistoryTableView.ContentInset = new UIEdgeInsets(0, 0, 10.0f, 0);
    }

    public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
        caseHistoryTableView.ReloadData();
        caseHistoryTableView.RowHeight = UITableView.AutomaticDimension;
        caseHistoryTableView.EstimatedRowHeight = 145.0f;
    }

    public class CaseHistorySourceClass : UITableViewSource
    {
        private List<CaseSearchItem> caseSearchItems;
        public CaseSearchItem caseSearchItem;
        public static event EventHandler RowClicked;
        public CaseHistorySourceClass(List<CaseSearchItem> caseSearchItems)
        {
            this.caseSearchItems = caseSearchItems;
        }
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            CaseHistoryTableCell cell = tableView.DequeueReusableCell(CaseHistoryTableCell.Key) as CaseHistoryTableCell ?? CaseHistoryTableCell.Create();
            var item = caseSearchItems[indexPath.Row];

            cell.BindData(item.Organization, item.Address, item.Doctor, item.UserName);
            cell.Layer.MasksToBounds = false;
            cell.Layer.CornerRadius = 10.0f;
            cell.BackgroundColor = UIColor.White;
            return cell;
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            caseSearchItem = caseSearchItems[indexPath.Row];
            tableView.DeselectRow(indexPath, true);
            if (RowClicked != null)
                RowClicked(caseSearchItem, EventArgs.Empty);
        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return caseSearchItems.Count;
        }
    }

}

TableViewCell.cs

public partial class CaseHistoryTableCell : UITableViewCell
{
     public static readonly NSString Key = new NSString("CaseHistoryTableCell");
     public static readonly UINib Nib;

     static CaseHistoryTableCell()
     {
          Nib = UINib.FromName("CaseHistoryTableCell", NSBundle.MainBundle);
     }

     public CaseHistoryTableCell(IntPtr handle) : base(handle)
     {
         // Note: this .ctor should not contain any initialization logic.
     }

     public static CaseHistoryTableCell Create()
     {
         return (CaseHistoryTableCell)Nib.Instantiate(null, null)[0];
     }

     public void BindData(string hospitalLabel, string addressLabel, string drLabel, string patientLabel)
     {
         this.lbl_hospitalName.Text = hospitalLabel;
         this.lbl_address.Text = addressLabel;

         this.lbl_drName.Text = drLabel;
         this.lbl_patientName.Text = patientLabel;

         this.lbl_address.TextColor = UIColor.Clear.FromHexString("#000000", 0.54f);
         this.lbl_patientName.TextColor = UIColor.Clear.FromHexString("#000000", 0.54f);
         this.lbl_hospitalName.TextColor = UIColor.Clear.FromHexString("#000000", 0.87f);
         this.lbl_drName.TextColor = UIColor.Clear.FromHexString("#000000", 0.87f);
            this.btn_createAppointment.SetTitleColor(UIColor.Clear.FromHexString("#0072BA", 1.0f), UIControlState.Normal);
            this.btn_viewDetail.SetTitleColor(UIColor.Clear.FromHexString("#0072BA", 1.0f), UIControlState.Normal);

       }

       public override CGRect Frame
       {
            get
            {
                return base.Frame;
            }

            set
            {
                value.Y += 4;
                value.Height -= 2 * 4;
                base.Frame = value;
            }
      }

 }

我想要的是:

我想让所有单元格高度动态化。但是每一次,第二个单元格都没有进行调整。

我尝试过的:

1.)

ViewDidAppear 中,我设置了以下内容:

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);
    caseHistoryTableView.ReloadData();
    caseHistoryTableView.RowHeight = UITableView.AutomaticDimension;
    caseHistoryTableView.EstimatedRowHeight = 145.0f;
}

2.)

GetCell 方法中,我以这种方式添加单元格:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
    CaseHistoryTableCell cell = tableView.DequeueReusableCell(CaseHistoryTableCell.Key) as CaseHistoryTableCell ?? CaseHistoryTableCell.Create();
    var item = caseSearchItems[indexPath.Row];
    cell.BindData(item.Organization, item.Address, item.Doctor, item.UserName);

    cell.Layer.MasksToBounds = false;
    cell.Layer.CornerRadius = 10.0f;
    cell.BackgroundColor = UIColor.White;
    cell.SetNeedsLayout();
    cell.LayoutIfNeeded();
    return cell;
}

但我的输出仍然低于:


第二个单元格不采用自动高度。

标签 1 约束:(医院标签)

标签 2 containt :(地址标签)

最佳答案

根据您对单行高度的要求添加两个标签的高度,现在为两个约束添加关系从 equalto(=)greaterthanorequalto(>=)

见下图。

现在运行并重新检查,如果现在遇到任何问题请告诉我。

关于ios - 表格 View 中单元格的动态高度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294976/

有关ios - 表格 View 中单元格的动态高度不起作用的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  4. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  5. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  6. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  7. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  8. ruby-on-rails - Prawn - 表格单元格内的链接 - 2

    我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c

  9. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  10. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

随机推荐