简体   繁体   English

Xamarin iOS如何在编辑时制作自定尺寸的UITableViewCell

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

I am trying to build an self-sizing UITableView Cell . 我正在尝试构建一个自定义大小的 UITableView Cell After googled, I found this tutorial: https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/ which is quite good. 谷歌搜索后,我找到了本教程: https : //pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/这是相当不错的。

In swift , it's saying that tableView ?. 很快 ,就是说tableView ?。 BeginUpdates can update the size of the custom cell. BeginUpdates可以更新自定义单元的大小。 But It seems not working in xamarin ios. 但似乎在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! 非常感谢有人可以指导我!

Based on constraints placed on view, then autolayout will work. 根据视图上的约束,自动布局将起作用。 The code works fine after I set up the constraints of each components. 在设置每个组件的约束之后,代码可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:在UITableViewCell中创建自调整大小的UITextView? - IOS:Creating a Self-Sizing UITextView Within a UITableViewCell? 如何在具有自调整大小的单元格的 UITableViewCell 中添加填充? - How to add padding inside a UITableViewCell with self-sizing cells? 没有自动布局的自调整 UITableViewCell - Self-sizing UITableViewCell without Auto Layout 自调整UITableViewCell的内部带有UITableView的功能-自调整不起作用 - Self-sizing UITableViewCell with UITableView inside it - self-sizing doesn't work UITableViewCell 扩展 animation 与自定尺寸标签,可视 animation 问题 - UITableViewCell expanding animation with self-sizing labels, visual animation problem 自定义UITableViewCell永远不会以AutoLayout自调整大小显示 - Custom UITableViewCell never showing up with AutoLayout self-sizing 如何使自定义基本UITableViewCell? - How to make a self sizing basic UITableViewCell? 如何:在iOS中使用StackView View从XIB自我调整自定义视图 - How To: Self-Sizing Custom-View from XIB with StackView View in iOS iOS AutoLayout可自动调整整个表格的高度(不是单元格!) - iOS AutoLayout self-sizing whole table (not cells!) height iOS 10中的collectionViewContentSize使用自调整单元格 - collectionViewContentSize in iOS 10 using self-sizing cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM