Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin ios how to make a self-sizing UITableViewCell while editing

I am trying to build an self-sizing UITableView Cell. After googled, I found this tutorial: https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/ which is quite good.

In swift, it's saying that tableView?.BeginUpdates can update the size of the custom cell. But It seems not working in xamarin ios.

Could someone help me on that? Many Thanks!

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

BTW, I am using autolayout and set

TableView.EstimatedRowHeight = 50;
TableView.RowHeight = UITableView.AutomaticDimension;

And I have done the following setting as well.

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

            return UITableView.AutomaticDimension;
        }

Many thanks if someone can guide me!

like image 472
J.L Avatar asked Dec 28 '15 01:12

J.L


1 Answers

Based on constraints placed on view, then autolayout will work. The code works fine after I set up the constraints of each components.

like image 72
J.L Avatar answered Nov 16 '22 01:11

J.L