iosxamarin.iosxamarinmonotouch.dialog

Custom EntryElement alignment changes on redraw / refresh


I've created a custom EntryElement for our MonoTouch.Dialog so that all the controls are aligned to the right. The code is as follows:

public class RightAlignEntryElement : EntryElement
{
   private NSString _cellKey = new NSString("RightAlignedEntryElement");

   public RightAlignEntryElement(string caption, string placeholder, string value) : base(caption, placeholder, value)
   {
   }

   protected override UITextField CreateTextField(RectangleF frame)
   {       
      var paddedFrame = frame;
      paddedFrame.Width = frame.Width -= 10;
      var textField = base.CreateTextField(paddedFrame);
      textField.TextAlignment = UITextAlignment.Right;


      return textField;
  }

  public override UITableViewCell GetCell(UITableView tv)
  {
     var cell = base.GetCell(tv);
     return cell;
  }

  protected override NSString CellKey 
  {
     get { return _cellKey; }
  }
}

this works fine, until the Dialog gets refreshed, for example the table is scrolled, or subview is presented then dismissed.

Before there is padding on the right of the cell, and once scrolled the padding disappears. I've seen that this has been a problem is the past, and have seen various other suggestions on here and the Xamarin forums, but nothing seems to work for me.

See the attached screen grabs for before and after.

Before

After


Solution

  • For the record, here is the response I got on another forum for this question:

    The behaviour is due to not using the cell reuse in you RightAlignEntryElement

    Your GetCell method should look something like:

    public override UITableViewCell GetCell(UITableView tv) 
    { 
       var cell = tv.DequeueReusableCell (CellKey) as UITableViewCell; 
       if (cell == null) { 
          cell = base.GetCell(tv); 
    
          cell.TextLabel.Font = UIFont.FromName("HelveticaNeue-Light", 16); 
       } 
    
       return cell; 
    }