作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在网上冲浪,想弄清楚如何让我的表格单元高度适合它的内容(我的内容高度不同)。
我试过查看这个样本.... http://simon.nureality.ca/simon-says-project-d-uitableviewcells-autosize-based-on-text-height-in-monotouch-heres-how/......但我无法让它发挥作用。
到目前为止,这是我的代码。单元格高度实际上有效,但文本以尾随点为中心(而不是钻出整个框架):
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
string item = this.Lst.Items [indexPath.Section];
UITableViewCell cell = tableView.DequeueReusableCell (_cellIdentifier);
if (cell == null) {
cell = new UITableViewCell (UITableViewCellStyle.Default, _cellIdentifier);
cell = new UITableViewCell (UITableViewCellStyle.Subtitle, _cellIdentifier);
}
// Find the height...
SizeF textWidth = new SizeF (tableView.Bounds.Width - 40, float.MaxValue);
SizeF textSize = tableView.StringSize(item, Font, textWidth, LineBreakMode);
// Textlabel...
cell.TextLabel.Text = item;
cell.TextLabel.Font = Font;
cell.TextLabel.Frame = new RectangleF(cell.TextLabel.Frame.X, cell.TextLabel.Frame.Y, textSize.Width, textSize.Height);
//cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
cell.ImageView.Image = PlaceholderImage;
cell.EditingAccessory = UITableViewCellAccessory.DisclosureIndicator;
// Sizing the cell...
RectangleF rectCell = cell.Frame;
rectCell.Height = textSize.Height;
cell.Frame = rectCell;
return cell;
}
public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
string item = this.Lst.Items [indexPath.Section];
SizeF size = new SizeF (tableView.Bounds.Width - 40, float.MaxValue);
float height = tableView.StringSize (item, Font, size, LineBreakMode).Height + 10;
return height;
}
它看起来像这样......
知道哪里出了问题吗?
谢谢!
魔力
最佳答案
您可以通过设置 Lines
和 LineBreakMode
属性来控制文本标签的行为。
例如设置
cell.TextLabel.Lines = 0;
cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap;
会让您的标签自动换行并根据需要使用尽可能多的行。
也可以考虑使用 MonoTouch.Dialog米格尔·德·伊卡萨 (Miguel De Icaza)。它大大简化了 UITableView 的处理。
关于uitableview - 单点触控 : UITableViewCell height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8344596/
我是一名优秀的程序员,十分优秀!