我在xamarin.ios新我想根据内容长度来调整行高。 我怎样才能做到这一点。
这里是我的ViewController.Cs
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
nfloat _width = UIScreen.MainScreen.Bounds.Width / 3;
DataBinding();
_table = new UITableView(new CGRect(0, 0, View.Frame.Width, View.Frame.Height));
_table.Source = new DetailsTableViewSource(_caseDesign);
_table.RowHeight = UITableView.AutomaticDimension;
_table.EstimatedRowHeight = 100f;
_table.ReloadData();
View.AddSubview(_table);
}
DetailsTableViewSource.Cs
public class DetailsTableViewSource : UITableViewSource
{
public List<CaseDetails> tabledata;
public DetailsTableViewSource(List<CaseDetails> items)
{
tabledata = items;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
NSString cellIdentifier = new NSString();
var cell = tableView.DequeueReusableCell(cellIdentifier) as CaseDetailsTableCell;
if (cell == null)
cell = new CaseDetailsTableCell(cellIdentifier);
cell.UpdateCell(tabledata[indexPath.Row].Title
, tabledata[indexPath.Row].Description
);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return tabledata.Count;
}
}
CaseDetailsTableCell.cs
public class CaseDetailsTableCell:UITableViewCell
{
UILabel _qst1, _answer1, _seperator;
nfloat _width;
public CaseDetailsTableCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
_width = UIScreen.MainScreen.Bounds.Width / 3;
_qst1 = new UILabel();
_qst1.Font = _qst1.Font.WithSize(15);
_seperator = new UILabel();
_seperator.TextAlignment = UITextAlignment.Center;
_seperator.Text = ":";
_seperator.Font = _seperator.Font.WithSize(20);
_answer1 = new UILabel();
_answer1.Font = _answer1.Font.WithSize(15);
ContentView.AddSubviews(new UIView[] { _qst1, _seperator, _answer1 });
}
public void UpdateCell(string Quetion, string Answer)
{
_qst1.Text = Quetion;
_answer1.Text = Answer;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
_qst1.Frame = new CGRect(10, 10, _width + 10, 30);
_qst1.Lines = 0;
_qst1.SizeToFit();
_qst1.LineBreakMode = UILineBreakMode.Clip;
_qst1.TextAlignment = UITextAlignment.Left;
_answer1.Frame = new CGRect(_width * 2, 10, UIScreen.MainScreen.Bounds.Width / 3 - 10, 30);
_answer1.SizeToFit();
_answer1.TextAlignment = UITextAlignment.Justified;
_answer1.LineBreakMode = UILineBreakMode.WordWrap;
_seperator.Frame = new CGRect(_width + 10, 10, UIScreen.MainScreen.Bounds.Width / 3, 30);
// _date.Frame = new CGRect(17, 50, 50, 20);
}
}
我要绑定一些数据插入表中和的每个项目是不同的长度,根据该数据的长度,行的高度被自动调整。 我怎样才能做到这一点。
现在,该表如下所示
我想这样的表