Monotouch:UITableViewCell 高度 [英] Monotouch: UITableViewCell height

查看:22
本文介绍了Monotouch:UITableViewCell 高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上冲浪,想弄清楚如何使我的表格单元高度适合其内容(我的内容具有不同的高度).

我已经尝试查看此示例.....它大大简化了 UITableView 的处理.

I've been surfing the net to figure out how make my tablecell height fitt its content (my content is of different height).

I've tried to look at this sample .... http://simon.nureality.ca/simon-says-project-d-uitableviewcells-autosize-based-on-text-height-in-monotouch-heres-how/ ... but I can't make it work.

Here's my code so far .. the cell height is actually working, but the text is centered with trailling dotts (instead of dilling out the entire frame):

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;
}

And it looks like this...

Any idea what is wrong?

Thanks!

Mojo

解决方案

You can control the textlabel's behavior by setting the Lines and LineBreakMode properties.

For example, setting

cell.TextLabel.Lines = 0;
cell.TextLabel.LineBreakMode = UILineBreakMode.WordWrap; 

will let your label do word wrapping and use as many lines as needed.

Also consider using MonoTouch.Dialog by Miguel De Icaza. It simplifies dealing with UITableView considerably.

这篇关于Monotouch:UITableViewCell 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆