簡體   English   中英

使用自動布局表格視圖時,如何增加表格視圖單元格中的標簽高度(取決於文本)?

[英]How to increase Label Height(Depend on Text) in Tableview cell when used Autolayout Table view?

我有一個使用自動布局和約束的視圖和表格視圖設計。 我已經成功使用Autolayout設計了視圖。

但是我對表格視圖單元格的內容還有其他要求。 **我想相應地增加標簽的高度文本(取決於標簽文本的內容-標簽1)。 請查看所附的屏幕截圖。 我已使用Storyboard將CELL設置為表格視圖。 表格視圖單元格進入了3個標簽。**

Label -2 :我設置了約束標簽2,前緣約束5,后緣約束5,上邊緣約束10,下約束0。 在這里,我設置Label-2 20的高度。 它固定不變,我不想增加它們。

Label -3 :我設置了約束標簽3,前緣約束5,后緣約束5,上邊緣約束0,下約束5。 在這里,我設置了Label-3 20的高度。 它固定不變,我不想增加它們。

我想根據文本增加標簽1的高度。

**_Label -1 :_** 我想增加Label 1的大小。我設置了約束label-1,前沿約束5,后沿約束5,上邊界約束5,下約束10。 在這里,我設置Label-1 35的高度。

目前,我尚未設置Label-1高度約束的約束。 我也嘗試過設置高度限制,但不能在增加高度的問題上生效。 我已經為Label-1創建了方法。 它是count標簽1的大小,並相應增加。 但這不是返回當前高度。

在這里,我設置了表格視圖單元格大小95。它將使用表格視圖高度委托方法動態增加。

_
源代碼 :-_

UITableViewSource方法:

//計算表格視圖單元格的高度

       public override nfloat GetHeightForRow (UITableView tableView, NSIndexPath indexPath){

        TableViewCellClass cell = null;
        cell = (TableViewCellClass)tableView.DequeueReusableCell(cellIdentifier);

        return cell.RequiredTableViewCellHeight(tableView.Bounds.Size.Width);
    }


   public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath){

        TableViewCellClass cell = (TableViewCellClass)tableView.DequeueReusableCell(cellIdentifier,indexPath);

        //Add Data In Cell
        cell.Updatecell (tableItems [indexPath.Row].Label1Data, tableItems [indexPath.Row].Label2Data, tableItems [indexPath.Row].Label3Data);

        return cell;

    }

TableView單元格方法:-

 partial class TableViewCellClass : UITableViewCell{

    public TableViewCellClass (IntPtr handle) : base (handle)
    {

    }

    public void Updatecell (string lbl1Data, string lbl2Data, DateTime lbl3Data){

        Label1.Text = lbl1Data; //I want to increases height of label1

        Label2.Text = lbl2Data;
        Label3.Text = lbl3Data.ToString();

        //Here Below, fixed text and size for Label-2 and Label-3. It was fixed and never increase it's height
        Label2.SizeToFit();
        Label2.AdjustsFontSizeToFitWidth = true;

        Label3.SizeToFit();
        Label3.AdjustsFontSizeToFitWidth = true;
    }

// RequiredTableViewCellHeight方法,用於對標簽高計數,並根據此方法增加單元格。

   public float RequiredTableViewCellHeight(nfloat screenWidth){

        Console.WriteLine("screenWidth : {0}",screenWidth); //Here we get the screen size of table view WIDTh

        //Here make dynamic lable
        Label1.Lines = 0;
        Label1.AdjustsFontSizeToFitWidth = false;

        UIFont cellFont = Label1.Font;

        nfloat lbl1widthVal = screenWidth - 10; //Here we Minus 10 due to leading and trailing constraint. So Width not effected.

        //Below Count the size of label height accordingly text and font size. Size not return actuall size. It's some time return always same.
        CGSize lbl1Size = ((NSString) Label1.Text).StringSize(cellFont,constrainedToSize:new CGSize(lbl1widthVal,400.0f),lineBreakMode:UILineBreakMode.WordWrap);

        Console.WriteLine("lbl1Size Total Size: {0}",lbl1Size);

        int rowHeight = (int)lbl1Size.Height; //Get the Height of table cell
        Console.WriteLine("rowHeight: {0}",rowHeight);

        rowHeight += 60; //Add extra 60 due to label-2, label-3 size and spacing value in cell
        Console.WriteLine("After Final Edit rowHeight value: {0}",rowHeight);

        return (float)rowHeight;
    }


}

我可以獲取由於自動版式配置引起的問題,還是在代碼中的某個地方犯了錯誤?
請注意,我不想在項目中刪除自動布局。 請對此進行審查,並讓我們知道您的建議。 預先感謝!

自定義視圖(tableviewcell)可能對您有用。

設置約束,然后設置tableView.estimatedRowHeight = 44.0(例如),然后設置tableView.rowHeight = UITableViewAutomaticDimension

這是有關此的教程: http : //www.appcoda.com/self-sizing-cells/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM