ios - Xamarin ios 如何在编辑时制作一个自适应大小的 UITableViewCell

标签 ios uitableview xamarin autolayout uitextview

我正在尝试构建一个自调整大小 UITableView Cell。谷歌搜索后,我找到了这个教程:https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/这很好。

swift 中,它说 tableView?.BeginUpdates 可以更新自定义单元格的大小。但它似乎不适用于 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);
            }
        }
    }
}

顺便说一句,我正在使用 autolayout 并设置

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

我也做了以下设置。

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

            return UITableView.AutomaticDimension;
        }

如果有人能指导我,非常感谢!

最佳答案

基于 View 上的约束,自动布局将起作用。在我设置每个组件的约束后,代码工作正常。

关于ios - Xamarin ios 如何在编辑时制作一个自适应大小的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34486674/

相关文章:

ios - 如何使用扩展来简化 Swift 代码?

尝试从相机添加附件时 iOS 11 应用程序崩溃

c# - Xamarin Forms Post 请求 Http 问题

ios - 从起始值到结束值绘制直线 ARKit

ios - 错误 "ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=i386"

ios - 将单元格的 subview 快速移动到前面

iphone - UITableView 滚动缓慢,内存泄漏问题

iOS - Dilemna - 如何从自定义单元格中分离出来

xamarin - 使用 Xamarin Forms 显示图像

iphone - NSHomeDirectory 删除所有文件