【问题标题】:Monotouch.Dialog changing x of elements frame gets repositioned after it goes off screen and back onMonotouch.Dialog 更改元素框架的 x 在它离开屏幕并重新打开后重新定位
【发布时间】:2013-12-17 17:28:52
【问题描述】:

我正在使用 Monotouch.Dialog 并且需要重新定位一些 EntryElements - 当它们在一个部分内时它们似乎会漂移。

我将框架设置为frame.X = frame.X - 5; 以解决此问题。但是,如果单元格离开屏幕,然后在框架上向后滚动,则似乎已重置并且 UI 看起来不合时宜。

我能做些什么来确保框架永远不会重置或至少恢复到应有的状态?

这是代码 - 这是使用 Monotouch.Dialog 的 MvvmCross 端口,因此 GetCell 现在是 GetCellImpl - 它的工作方式几乎相同 - 在它的基类 (EntryElement) 中调用 dequeuereusablecell,我正在修改无论如何,在那之后的单元格......所以不应该工作吗?:

public class MyEntryElement : EntryElement, IElementSizing
{
    public bool Disabled {
        get;
        set;
    }


    public MyClaimEntryElement()
        : base()
    {
    }

    public MyEntryElement(string caption)
        : base(caption)
    {
    }

    public MyEntryElement(string caption, string placeholder)
        : base(caption, placeholder)
    {
    }

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

    public MyEntryElement(string caption, string placeholder, string value, bool isPassword)
        : base(caption, placeholder, value, isPassword)
    {
    }

    protected override UITableViewCell GetCellImpl(UITableView tv)
    {
        var cell = base.GetCellImpl(tv);
        cell.BackgroundColor = UIColor.Clear;
        UITextField uiTextField;

        if (cell.ContentView.Subviews.Length == 1)
        {
            uiTextField = ((UITextField)cell.ContentView.Subviews[0]);
        }
        else
        {
            uiTextField = ((UITextField)cell.ContentView.Subviews[1]);
        }

        if (cell.ContentView.Subviews.Length == 2) {
            var labelField = ((UILabel)cell.ContentView.Subviews [0]);
            labelField.TextColor = UIColor.White;
        }
        uiTextField.Enabled = Disabled;
        uiTextField.TextColor = UIColor.Black;
        uiTextField.Background = UiHelper.Text_Bubble;
        uiTextField.Enabled = !Disabled;
        var frame = uiTextField.Frame;
        frame.X = frame.X - 5;
        frame.Height = frame.Height + 10;
        uiTextField.Frame = frame;
        return cell;
    }

    private const float CellHeight = 40; //standard height. You can make it bigger or smaller, tho smaller means fingers miss easier.

    public float GetHeight(UITableView tableView, NSIndexPath indexPath)
    {
        return CellHeight;
    }
}

【问题讨论】:

  • 你需要展示一些代码。单元格被重复使用(即缓存),因此任何离开屏幕的单元格可能与您返回的单元格不同。 IOW 在哪里设置框架很重要。
  • 我通过将 X 设置为静态值而不是计算来解决此问题

标签: ios uitableview xamarin.ios xamarin monotouch.dialog


【解决方案1】:

由于UITableViewCell 被重复使用,根据它以前的值修改它的Frame 属性会导致问题。

例如

  • 您在表格视图中显示了 12 个单元格。它们都是可见的,需要创建;

  • 您的代码从 X 位置减去 5。如果原来的 X 是 20,那么现在是 15;

  • 滚动然后创建一些新单元格(X 将再次为 20 - 5 = 15),但大多数情况下,不再可见的单元格将被回收;

  • 您的代码再次从 X 位置减去 5。 X == 15 的任何单元格现在都为 10;

这就是为什么将 Frame.X 设置为静态字段值(不基于当前 X)有效的原因。

注意:如果您在调试器中运行此程序并检查 Frame 是否每次调用 GetCellImpl,上述内容会更容易理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多