Xamarin ios如何在编辑时制作自定义UITableViewCell

J.L*_*J.L 8 uitableview uitextview ios autolayout xamarin

我正在努力建立一个自我调整大小 UITableView Cell.谷歌搜索后,我找到了这个教程:https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/这是非常好的.

快速,它说的是tableViewBeginUpdates可以更新自定义单元格的大小.但它似乎不适用于xamarin ios.

有人可以帮助我吗?非常感谢!

using System;
using Foundation;
using UIKit;
using CoreGraphics;

namespace Ma
{
    public partial class DataInput : UITableViewCell
    {
        public string title { get; set;}
        public static readonly UINib Nib = UINib.FromName ("DataInput", NSBundle.MainBundle);
        public static readonly NSString Key = new NSString ("DataInput");

        public string value { get; set;}

        public DataInput (IntPtr handle) : base (handle)
        {

        }

        public static DataInput Create ()
        {
            return (DataInput)Nib.Instantiate (null, null) [0];
        }

        public void Populate()
        {
            this.Title.Text = this.title;
            if (!string.IsNullOrEmpty(value)) {
                this.Input.Text = this.value;
            }
        }

        public string GetInputValue()
        {
            return this.Input.Text;
        }

        public UITableView GetTableView()
        {
            UITableView table = null;

            UIView view = this.Superview;
            if (view != null) {
                table = (UITableView)view.Superview;                    
            }

            return table;
        }

        public override void AwakeFromNib ()
        {
            base.AwakeFromNib ();
            this.Input.ScrollEnabled = false;
            this.Input.Delegate = new DataInputDelegate ();
        }

        public override void SetSelected (bool selected, bool animated)
        {
            base.SetSelected (selected, animated);

            if (selected) {
                this.Input.BecomeFirstResponder ();
            } else {
                this.Input.ResignFirstResponder ();
            }
        }
    }

    public partial class DataInputDelegate : UITextViewDelegate
    {
        public override void Changed (UITextView textView)
        {
            var size = textView.Bounds.Size;
            var newSize = textView.SizeThatFits (new CGSize (size.Width, size.Height));

            if (size.Height != newSize.Height) {
                UITextView.AnimationsEnabled = false;

                UITableViewCell input = (UITableViewCell)textView.Superview.Superview;
                UITableView tableView = (UITableView)input.Superview.Superview;

                // This is the place of updating custom cell size, but It's not working now. 
                tableView.BeginUpdates ();
                tableView.EndUpdates ();
                UITextView.AnimationsEnabled = true;

                var thisIndexPath = tableView.IndexPathForCell (input);
                tableView.ScrollToRow (thisIndexPath, UITableViewScrollPosition.Bottom, false);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我正在使用autolayout和set

TableView.EstimatedRowHeight = 50;
TableView.RowHeight = UITableView.AutomaticDimension;
Run Code Online (Sandbox Code Playgroud)

我也做了以下设置.

public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            if (indexPath.Row == 0) {
                return 80.0f;
            }

            return UITableView.AutomaticDimension;
        }
Run Code Online (Sandbox Code Playgroud)

非常感谢有人可以指导我!

J.L*_*J.L 4

根据视图上的约束,自动布局将起作用。在我设置每个组件的约束后,代码运行良好。