2015-12-28 14 views
8

संपादित करते समय एक स्व-आकार देने वाला UITableViewCell कैसे बनाएं I स्वयं-आकारUITableView Cell बनाने का प्रयास कर रहा हूं। गुगल होने के बाद, मुझे यह ट्यूटोरियल मिला: https://pontifex.azurewebsites.net/self-sizing-uitableviewcell-with-uitextview-in-ios-8/ जो काफी अच्छा है।Xamarin ios

स्विफ्ट, यह कह रहा है कि 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); 
      } 
     } 
    } 
} 

Btw, मैं 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; 
     } 

कोई धन्यवाद अगर कोई मुझे मार्गदर्शन कर सकता है!

उत्तर

4

दृश्य पर रखी गई बाधाओं के आधार पर, ऑटोलायआउट काम करेगा। प्रत्येक घटक की बाधाओं को स्थापित करने के बाद कोड ठीक काम करता है।