简体   繁体   English

使用自动布局表格视图时,如何增加表格视图单元格中的标签高度(取决于文本)?

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

I have a view and table view design using Autolayout and Constraints. 我有一个使用自动布局和约束的视图和表格视图设计。 I have successfully design view using Autolayout. 我已经成功使用Autolayout设计了视图。

But I have one more requirement in table view cell content. 但是我对表格视图单元格的内容还有其他要求。 ** I want to increase LABEL height accordingly text (Depend on content of label text- Label 1). **我想相应地增加标签的高度文本(取决于标签文本的内容-标签1)。 Please review attached screen shot. 请查看所附的屏幕截图。 I have set CELL into the table view using Storyboard. 我已使用Storyboard将CELL设置为表格视图。 Table view cell into I have taken 3 Labels.** 表格视图单元格进入了3个标签。**

Label -2 : I have set constraint label-2, Leading edge constraint-5, Trailing edge constraint-5, Top edge constraint-10, Bottom constraint-0. Label -2 :我设置了约束标签2,前缘约束5,后缘约束5,上边缘约束10,下约束0。 Here I set height of Label-2 20. It fixed and I do not want to increase them. 在这里,我设置Label-2 20的高度。 它固定不变,我不想增加它们。

Label -3 : I have set constraint label-3, Leading edge constraint-5, Trailing edge constraint-5, Top edge constraint-0, Bottom constraint-5. Label -3 :我设置了约束标签3,前缘约束5,后缘约束5,上边缘约束0,下约束5。 Here I set height of Label-3 20. It fixed and I do not want to increase them. 在这里,我设置了Label-3 20的高度。 它固定不变,我不想增加它们。

I want to increase height of Label 1 according to text. 我想根据文本增加标签1的高度。

**_Label -1 :_** I want to increase size of Label 1. I have set constraint label-1, Leading edge constraint-5, Trailing edge constraint-5, Top edge constraint-5, Bottom constraint-10. **_Label -1 :_** 我想增加Label 1的大小。我设置了约束label-1,前沿约束5,后沿约束5,上边界约束5,下约束10。 Here I set height of Label-1 35. 在这里,我设置Label-1 35的高度。

Right now, I have not set constraint of Label-1 height constraint. 目前,我尚未设置Label-1高度约束的约束。 I have also tried set height constraint but it's cannot take effect on height increases issues. 我也尝试过设置高度限制,但不能在增加高度的问题上生效。 I have created method for Label-1. 我已经为Label-1创建了方法。 It was count label-1 size and increases it accordingly. 它是count标签1的大小,并相应增加。 But it was not return current height. 但这不是返回当前高度。

Here, I set table view cell size 95. And it will be increase dynamically using table view height delegate method. 在这里,我设置了表格视图单元格大小95。它将使用表格视图高度委托方法动态增加。

_ _
Source Code :-_ 源代码 :-_

UITableViewSource Method: UITableViewSource方法:

//Count Height of table view cell //计算表格视图单元格的高度

       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 Cell Method:- 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 Method For Count Label Hight and Increase Cell According to it. // 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;
    }


}

Can i fetch issues due to autolayout or I do make mistake somewhere in code? 我可以获取由于自动版式配置引起的问题,还是在代码中的某个地方犯了错误?
Please note that, I do not want to remove autolayout in project. 请注意,我不想在项目中删除自动布局。 Please review that and let us know your suggestions. 请对此进行审查,并让我们知道您的建议。 Thank in advance! 预先感谢!

Self sizing views (tableviewcells) might be of use to you. 自定义视图(tableviewcell)可能对您有用。

You set the constraints, then set tableView.estimatedRowHeight = 44.0 (for example) Then set tableView.rowHeight = UITableViewAutomaticDimension 设置约束,然后设置tableView.estimatedRowHeight = 44.0(例如),然后设置tableView.rowHeight = UITableViewAutomaticDimension

Here is a tutorial about this: http://www.appcoda.com/self-sizing-cells/ 这是有关此的教程: http : //www.appcoda.com/self-sizing-cells/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元格高度不增加取决于表格视图 swift 中的 label 文本 - Cell height is not increasing depend on the label text in tableview swift 标签文本增加时,如何以编程方式增加集合视图单元格的高度? - How to programmatically increase collection view cell height when label text increases? 如何在表格视图单元格中增加文本标签和详细文本标签之间的空间? - How to increase space between text label and detail text label in table view cell? 在运行时设置标签文本多行时如何在tableview单元格内设置单元格的高度 - How to set height of cell inside tableview cell when set label text multiline at run time 当其中的 label 文本被修改时,有没有办法改变 tableView 单元格的高度? - Is there a way to change tableView cell's height, when the label text in it is modified? 使用自动布局将表格视图单元格的高度设置为0 - Give 0 height to table view cell using autolayout 如何增加表格视图单元格内的表格视图高度 - How to increase table view height inside table view cell 如何通过目标c(用于“对话页面”)增加表视图中的标签或文本视图大小(高度和宽度),您能帮我吗? - How to increase label or text view size (height and width) in tableview by objective c(For Conversation Page) Can you Help me? 如何在单击时增加/减少TableView单元格高度 - How to increase/decrease TableView cell height on click 滑动以显示Tableview单元格中的编辑操作时,如何保持文本标签的可见性? - How to keep text label in view when swiping to reveal edit actions in tableview cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM