2016-12-23 11 views
2

하나의 라벨과 2 개의 버튼이있는 table view을 만들었습니다. 나는 자동 레이아웃을 사용하고있다. 그리고 서버에서 데이터를 가져오고 있습니다.테이블 뷰의 셀의 동적 높이가 작동하지 않습니다.

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; 
      } 
     } 

} 

내가 원하는 것은이 :

나는 모든 셀 높이 동적으로 만들고 싶어. 그러나 매번 두 번째 셀은 조정을하지 않습니다.

내가 시도 무엇 :

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

2) GetCell에서

:

1) ViewDidAppear에서

, 나는 다음과 같은 설정 방법, 나는 세포를 이런 식으로 추가

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

라벨이 constaint : (주소 라벨)

enter image description here

+0

모든 것은 'autolayout'과'tableviewdelegates'로 관리 할 수 ​​있습니다. –

+0

@TinuDahiya 제 질문에서 말하는 것은'autoLayout'을 사용하고 있지만 작동하지 않습니다. 다시 질문하십시오. – Ironman

+0

레이블 높이 관계를'equalto (=)'에서'greaterthatequalto (> =)'로 변경하십시오. 그리고 나는 이미 당신의 질문을 읽었습니다. 어떤 제약에도 문제가 있어야합니다. –

답변

2

양쪽 레이블 높이를 추가, 한 줄의 높이에 대한 귀하의 요구 사항에 따라, 지금

은 아래를 참조 greaterthanorequalto(>=)equalto(=)에서 제약 모두에 관계를 추가 희망 영상.

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 메서드를 호출하십시오. –