2016-12-23 128 views
2

我創建了一個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 constaint:(地址標籤)

enter image description here

+0

整個事情可以通過'autolayout'和'tableviewdelegates'來管理 –

+0

@TinuDahiya正如我在我的問題中所說的,我正在使用'autoLayout',但它不工作再次看到問題。 – Ironman

+0

將標籤高度關係從'equalto(=)'更改爲'greaterthatequalto(> =)'。而且我已經讀過你的問題,在任何約束下都必須有問題。 –

回答

2

添加兩個標籤的高度,按照單行高度的要求,現在無論是從equalto(=)約束添加關係greaterthanorequalto(>=)

見下文圖片。

enter image description here

立即執行,並重新檢查,如果面對任何問題,現在不是讓我知道。

+0

等待我嘗試並很快回來。 – Ironman

+0

不要忘記你的標籤行屬性是從故事板0。在原型單元中,每個標籤和視圖都必須具有頂層和底層約束。並且如果所有這些組件都在UIView中,那麼請使用該UIView給予關係高度@Ironman –

+0

@Tihu Dahiya酷酷的工作Nice。 – Ironman

0

只需根據將顯示在那裏的字符串大小計算標籤的高度,只需添加以下方法即可獲得高度並相應地管理標籤大小。

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 
    } 

希望工程:)

+0

我嘗試你的代碼,但它不工作。 – Ironman

+0

你必須計算所有標籤的高度,然後將它們加在一起,並將其傳遞到heightForRowAtIndexPath, –

+0

在cellForRow的初始化之前調用heightForWithFont方法 –

相關問題