Xamarin.ios 在自定义Monotouch.Dialog元素中居中单元格内容

Xamarin.ios 在自定义Monotouch.Dialog元素中居中单元格内容,xamarin.ios,monotouch.dialog,Xamarin.ios,Monotouch.dialog,我正在编写一个generig Monotouch.Dialog元素来显示图像(不显示图像选择器),我正在重用UIViewElement和ImageElement中的代码,添加了一些参数,以便可以在登录屏幕中使用,例如(显示公司徽标等) 到目前为止,我有: public class ImageViewElement : Element, IElementSizing { protected UIImage Image; public CellFlags Flags = 0;

我正在编写一个generig Monotouch.Dialog元素来显示图像(不显示图像选择器),我正在重用UIViewElement和ImageElement中的代码,添加了一些参数,以便可以在登录屏幕中使用,例如(显示公司徽标等)

到目前为止,我有:

public class ImageViewElement : Element, IElementSizing
{
    protected UIImage Image;
    public CellFlags Flags = 0;
    public UIViewContentMode Alignment;

    public enum CellFlags {
        Transparent = 1,
        DisableSelection = 2,
        Both = 3
    }

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="caption">
    /// The caption, only used for RootElements that might want to summarize results
    /// </param>
    /// <param name="view">
    /// The view to display
    /// </param>
    /// <param name="transparent">
    /// If this is set, then the view is responsible for painting the entire area,
    /// otherwise the default cell paint code will be used.
    /// </param>
    public ImageViewElement (UIImage image, bool transparent, bool selectable, UIViewContentMode alignment) : base ("")
    {
        this.Alignment = alignment;
        this.Image = image;

        if(transparent)
            {Flags+=1;}
        if(!selectable)
            {Flags+=2;}
    }


    public override UITableViewCell GetCell (UITableView tv)
    {
        var cell = tv.DequeueReusableCell (CellKey);
        if (cell == null)
        {
            cell = new UITableViewCell (UITableViewCellStyle.Default, CellKey);
            cell.ContentMode = UIViewContentMode.BottomRight;
            switch(Flags)
            {
            case CellFlags.Transparent:
                cell.BackgroundColor = UIColor.Clear;
                //
                // This trick is necessary to keep the background clear, otherwise
                // it gets painted as black
                //
                cell.BackgroundView = new UIView (RectangleF.Empty) {BackgroundColor = UIColor.Clear};
                break;
            case CellFlags.DisableSelection:
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;
                break;
            case CellFlags.Both:
                cell.BackgroundColor = UIColor.Clear;
                //
                // This trick is necessary to keep the background clear, otherwise
                // it gets painted as black
                //
                cell.BackgroundView = new UIView (RectangleF.Empty) {
                    BackgroundColor = UIColor.Clear};
                cell.SelectionStyle = UITableViewCellSelectionStyle.None;
                break;
            }
            cell.ImageView.ContentMode = Alignment;
            cell.ContentMode = Alignment;
            cell.ImageView.Image = Image;

        }
        return cell;
    }
    public float GetHeight (UITableView tableView, NSIndexPath indexPath)
    {
        return Image.Size.Height;
    }
}
现在我的ContentView边界与单元格边界相同,0,0320,40,它们不正常,但我只查找,我的测试边界为0,0256256,但对齐仍然不起作用。。。不知如何是好


编辑:一些进步

我询问了关于IRC的建议

(2012-11-13 10:10:12) CENSORED: Implement a subclass of UITableViewCell
(2012-11-13 10:10:26) CENSORED: add the Image as a subview in the init method of the new class
(2012-11-13 10:10:33) CENSORED: and override layouSubviews
(2012-11-13 10:10:36) CENSORED: with the line:
(2012-11-13 10:10:56) CENSORED: imageView.Center = this.Center;
(2012-11-13 10:11:14) CENSORED: (call super first as well)
(2012-11-13 10:11:25) CENSORED: that way the image will always remain centred in the cell

(2012-11-13 10:38:23) CENSORED: AndreSM: UITableViewCell already has a UIImageView property (named ImageView)
(2012-11-13 10:38:36) CENSORED: you can just use that instead of creating a new one
所以我继续创建了一个子类:

public class ImageViewCell: UITableViewCell
{
    UIImage cellImage;

    public ImageViewCell (UIImage image, NSString key) : base(UITableViewCellStyle.Default, key)
    {
        cellImage = image;
        ImageView.Image = cellImage;
    }

    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();
        ImageView.Center = ContentView.Center;
        ImageView.SetNeedsDisplay ();
    }
}
但图像仍然有点偏离中心,这是一个屏幕截图,你们可以理解我的意思:

我不知道原因,我检查了什么是新的中心,值是正确的(X轴上的160…)


问题在于中心是在父坐标空间中定义的。因此ContentView的中心属性是在其父对象(UITableViewCell,它本身就是一个视图)的空间中定义的。但是,ImageView中心是在ImageView父对象(即ContentView)的坐标空间中定义的

此代码应通过计算ContentView自身坐标空间中的中心来解决此问题

public override void LayoutSubviews ()
{
    base.LayoutSubviews ();
    ImageView.Center = new PointF(ContentView.Bounds.Width/2,ContentView.Bounds.Height/2);   
    ImageView.SetNeedsDisplay ();
}

那么,你到底想实现什么,对齐图片或设置新图像(我假设对齐?)对齐,应该很简单,但图像不居中,我只希望它位于中心。这真的很奇怪,因为坐标是正确的,中心不应该是160。屏幕宽度为320,内容视图从左侧和右侧插入。因此,当单元格显示时,contentview宽度不能为320,但应更小
public override void LayoutSubviews ()
{
    base.LayoutSubviews ();
    ImageView.Center = new PointF(ContentView.Bounds.Width/2,ContentView.Bounds.Height/2);   
    ImageView.SetNeedsDisplay ();
}