基于uilabel的动态uitableviewcell
与Xamarin.ios一起工作,我需要我的UITATION ViewCell根据Uilabel的大小(文本数)来调整其高度。我有一个自定义的UitaiteViewCell,可以在其中创建Uilabel,并且基于用户输入设置文本。我真正知道如何使用public Override Nint RowsInsection(Uaithitview TableView,NINT部分)
method方法,我真正知道如何在UITATIONVIEWSOURCE类中调整单元格的高度。
public class ChatTableSource : UITableViewSource
{
public List<MessageDetails> messages;
public ChatTableSource()
{
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
CustomMessageCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomMessageCell;
// if there are no cells to reuse, create a new one
if (cell == null)
cell = new CustomMessageCell(cellIdentifier);
tableView.Layer.BackgroundColor = UIColor.White.CGColor;
cell.UpdateCell(messages[indexPath.Row].body);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return messages.ToArray().Length;
}
//public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
//{
// return dynamic size
//}
}
public class CustomMessageCell : UITableViewCell
{
UILabel body;
public CustomMessageCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
body = new UILabel();
ContentView.Add(body);
}
public void UpdateCell(string body)
{
this.body.Text = body;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
body.Frame = new CGRect(distanceFromScreenEdge, ContentView.Bounds.Top, longTextSize, ContentView.Bounds.Height);
body.Lines = 0;
}
}
Working with Xamarin.ios and I need my UITableViewCell to adjust its height based on the size (number of lines of text) of a UILabel. I have a custom UITableViewCell where the UILabel is created and the text is set based on user input. The only way I really know how to adjust the height of the cell is in the UITableViewSource class using the public override nint RowsInSection(UITableView tableview, nint section)
method.
public class ChatTableSource : UITableViewSource
{
public List<MessageDetails> messages;
public ChatTableSource()
{
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
CustomMessageCell cell = tableView.DequeueReusableCell(cellIdentifier) as CustomMessageCell;
// if there are no cells to reuse, create a new one
if (cell == null)
cell = new CustomMessageCell(cellIdentifier);
tableView.Layer.BackgroundColor = UIColor.White.CGColor;
cell.UpdateCell(messages[indexPath.Row].body);
return cell;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return messages.ToArray().Length;
}
//public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
//{
// return dynamic size
//}
}
public class CustomMessageCell : UITableViewCell
{
UILabel body;
public CustomMessageCell(NSString cellId) : base(UITableViewCellStyle.Default, cellId)
{
body = new UILabel();
ContentView.Add(body);
}
public void UpdateCell(string body)
{
this.body.Text = body;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
body.Frame = new CGRect(distanceFromScreenEdge, ContentView.Bounds.Top, longTextSize, ContentView.Bounds.Height);
body.Lines = 0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不使用Xamarin - 所以我无法测试 - 但这应该是您想做的...
使用约束,让自动layout为您处理尺寸。
您根本不需要
layoutsubviews()
。I don't work with Xamarin - so I can't test this - but this should be what you want to do...
Use constraints and let auto-layout handle the sizing for you.
You don't need
LayoutSubviews()
at all.