1

我有一个源自 MT.D 的控件StringElement。可以使用空白/空来创建元素Caption,随后当用户将文本添加到不同的控件时会更新该元素。Caption是 MT.DElement类中的一个字段,设置它不会自动更新关联的控件。因此,为了尝试更新控件,我创建了一个更新基本字段的属性,然后尝试更新控件。

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

可悲的是,它没有用新值更新 UI。使用调试器我可以看到它正确设置了新值,但它没有显示文本。如果我使用非空白创建控件,Caption则它会正确显示。我使用类似的方法来更新ImageView正常工作的控件。

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

知道为什么它对细胞不起作用TextLabel吗?

4

2 回答 2

3

我以前见过这种情况,需要重新布置单元格,因为需要更改 TextLabel 的框架以适应文本的变化。

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();
            }
        }
    }
}
于 2013-02-10T03:23:48.997 回答
0

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.

于 2013-02-11T00:00:59.363 回答