Monotouch: UITableViewCell height

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 .... ... 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?




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.

Need Your Help

Find elements in cell array (logical 0 or 1)

matlab cell logical

I have a cell column vector of stock symbols that are all three letters long. I need to create a column vector of the same length that returns a 1 when the symbol XYZ is present, and 0 when it is n...

Inheriting UI components from a parent UIViewController class?

iphone interface uiviewcontroller monotouch monodevelop

I have a custom UIViewController class that contains my similar methods and UI components shared by my other UIViewController classes. I was wondering if it's possible for these other classes to not

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.