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

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

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

我创建了一个 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;
}

但我仍然得到以下输出:


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

enter image description here

标签 1 约束医院标签)

enter image description here

标签 2 约束地址标签)

enter image description here



Best Answer-推荐答案


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

见下图。

enter image description here

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

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

回复

使用道具 举报

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

本版积分规则

关注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