Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 表视图中单元格的动态高度不工作_Ios_Uitableview_Xamarin_Xamarin.ios_Row Height - Fatal编程技术网

Ios 表视图中单元格的动态高度不工作

Ios 表视图中单元格的动态高度不工作,ios,uitableview,xamarin,xamarin.ios,row-height,Ios,Uitableview,Xamarin,Xamarin.ios,Row Height,我创建了一个表视图,它有6个标签和2个按钮。我正在使用自动布局。我正在从服务器获取数据 ViewController.cs public partial class CaseHistoryController : UIViewController { private List<CaseSearchItem> caseSearchItems; CaseHistorySourceClass caseSearchSourceclass; public CaseHi

我创建了一个
表视图
,它有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;
        }
    }

}
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。)

在查看显示中,我设置了以下内容:

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构造:(地址标签)


只需根据上面显示的字符串大小计算标签的高度,只需添加以下方法即可获得高度并相应地管理标签大小

 func heightForWithFont(font: UIFont, width: CGFloat, insets: UIEdgeInsets) -> CGFloat {

        let label:UILabel = UILabel(frame: CGRect(x:0, y:0, width: width + insets.left + insets.right, height: CGFloat.greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.lineBreakMode = NSLineBreakMode.byWordWrapping
        label.font = font
        label.sizeToFit()
        return label.frame.height + insets.top + insets.bottom
    }

希望它能起作用:)

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

见下图


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

通过
autolayout
tableviewdelegates
@TinuDahiya可以管理整个问题,正如我在问题中所说的,我正在使用
autolayout
,但它不起作用。请再次查看问题。将标签高度关系从
equalto()
greaterthatequalto(>=)
。我已经看过你的问题了,任何约束都有问题。你在情节提要中设置了numberOfLine=0吗?请分享您的自动布局屏幕截图。@DheerajD是的,我已在<代码>情节提要中设置。请稍等,我会尽快回来。别忘了您的标签线属性是“情节提要”中的0。原型单元中的每个标签和视图都必须根据其层次结构具有顶部和底部约束。如果所有这些组件都在UIView中,则与UIView@IronMan建立高度关系。您必须计算所有标签的高度,然后将它们相加,并将其传递到HeightForRowIndexPath中,在初始化单元格之前,在cellForRow中调用heightForWithFont方法