【发布时间】:2015-09-26 09:04:15
【问题描述】:
我有一个使用自动布局和约束的视图和表格视图设计。我已经使用 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。它已修复,我不想增加它们。
我想根据文字增加Label 1的高度。
**_Label -1 :_** 我想增加标签 1 的大小。我设置了约束标签 1、前缘约束 5、后缘约束 5、上缘约束 5 , 底部约束-10。这里我将 Label-1 的高度设置为 35。
目前,我还没有设置 Label-1 高度约束的约束。我也尝试过设置高度约束,但它不能对高度增加问题生效。 我已经为 Label-1 创建了方法。它是计数 label-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;
}
}
我可以获取由于自动布局引起的问题吗?或者我确实在代码中的某个地方出错了?
请注意,我不想删除项目中的自动布局。
请查看并告诉我们您的建议。提前致谢!
【问题讨论】:
标签: ios uitableview xamarin xamarin.ios autolayout