David Clarke
David Clarke

Reputation: 13256

StringElement-derived control is not updating caption

I have a control that is derived from MT.D StringElement. The element can be created with a blank/empty Caption which is subsequently updated when the user adds text to a different control. Caption is a field in the MT.D Element class and setting it doesn't automatically update the associated control. So in order to try to update the control I created a property that updates the base field and then attempts to update the control.

public new string Caption {
    get {
        return base.Caption;
    }
    set {
        base.Caption = value;
        var cell = GetActiveCell();
        if (cell != null) {
            cell.TextLabel.Text = value;
        }
    }
}

Sadly it's not updating the UI with the new value. Using the debugger I can see it sets the new value correctly but it's not displaying the text. If I create the control with a non-blank Caption then it is displayed correctly. I use a similar approach to update the control's ImageView which works correctly.

private void SetUiState(){
    var cell = this.GetActiveCell();
    if (cell != null) {
        var imgView = cell.ImageView;
        if (imgView != null) {
            imgView.Image = _isEnabled ? _enabledImage : _disabledImage;
        }
        cell.SelectionStyle = _isEnabled ? UITableViewCellSelectionStyle.Blue : UITableViewCellSelectionStyle.None;
    }
}

Any idea why it doesn't work for the cell's TextLabel?

Upvotes: 1

Views: 204

Answers (2)

miguel.de.icaza
miguel.de.icaza

Reputation: 32694

In addition to the answer from @Greg Munn, you want to make sure that you return a unique ID for your element. Currently you are not, so when UITableView dequeues a cell of TestElement mixed with StringELement, it would reuse cells that were not ready to be reused in the way they are.

Upvotes: 0

Greg Munn
Greg Munn

Reputation: 526

I've seen this before, the cell needs to be re-laid out because the frame of the TextLabel needs to be changed to accomodate the change in text.

public class TestElement : StringElement
{
    public TestElement() : base("")
    {

    }

    public new string Caption {
        get 
        {
            return base.Caption;
        }

        set 
        {
            base.Caption = value;
            var cell = GetActiveCell();
            if (cell != null) 
            {
                cell.TextLabel.Text = value;
                cell.SetNeedsLayout();
            }
        }
    }
}

Upvotes: 3

Related Questions