通常,iOS 8应该能够计算单元格本身的高度。到目前为止,此方法一直有效,但是如果我旋转设备,则该单元会恢复到其标准高度44点。
它应该是这样的:
旋转后的样子如下:
旋转后,它将保持该布局。它不再计算实际高度。我不知道为什么我已经添加了完整的代码。对于约束条件,请查看updateConstraints
。该代码在C#中,但是您应该可以阅读它。您当然可以在Objective-C中发布您的解决方案。
VC viewDidLoad
:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.TableView.Source = new TestSource (this);
this.TableView.RegisterClassForCellReuse (typeof(CustomCell), TestSource.cellIdentifier);
//this.TableView.RowHeight = 83;
}
我的资料来源:
public class TestSource : UITableViewSource
{
public static readonly NSString cellIdentifier = new NSString("CustomCell");
private TestTableVC controller;
public TestSource (TestTableVC controller)
{
this.controller = controller;
}
public override int RowsInSection (UITableView tableview, int section)
{
return 10;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
CustomCell cell = tableView.DequeueReusableCell (cellIdentifier) as CustomCell;
cell.UpdateCell ("Max Mustermann", "m", new DateTime (2014, 12, 10), "1234567890");
cell.SetNeedsUpdateConstraints ();
cell.UpdateConstraintsIfNeeded ();
return cell;
}
}
我的
CustomCell
:public class CustomCell : UITableViewCell
{
public static readonly NSString cellIdentifier = new NSString("CustomCell");
private UILabel fullNameLabel, genderLabel, birthdateLabel, iNumberLabel;
private bool didSetupConstraints = false;
public CustomCell ()
{
this.CreateView ();
}
public CustomCell (IntPtr handle) : base(handle)
{
this.CreateView ();
}
public void UpdateCell (string fullName, string gender, DateTime? birthdate, string iNumber)
{
fullNameLabel.Text = fullName;
genderLabel.Text = "Gender" + ": " + gender;
birthdateLabel.Text = "Born on" + ": " + dateOfBirth.ToString ("dd.MM.yyyy");
iNumberLabel.Text = iNumber;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
this.ContentView.SetNeedsLayout();
this.ContentView.LayoutIfNeeded();
}
public override void UpdateConstraints()
{
if (!didSetupConstraints) {
NSMutableDictionary viewsDictionary = new NSMutableDictionary ();
viewsDictionary ["fullNameLabel"] = fullNameLabel;
viewsDictionary ["genderLabel"] = genderLabel;
viewsDictionary ["birthdateLabel"] = birthdateLabel;
viewsDictionary ["iNumberLabel"] = iNumberLabel;
fullNameLabel.TranslatesAutoresizingMaskIntoConstraints = false;
genderLabel.TranslatesAutoresizingMaskIntoConstraints = false;
birthdateLabel.TranslatesAutoresizingMaskIntoConstraints = false;
iNumberLabel.TranslatesAutoresizingMaskIntoConstraints = false;
// Sizing of content view is differently in iOS 7 (autoresizing mask) and iOS 8 (layoutSubViews).
// Don't know why this occurs but it should only concern nibs and if you are using iOS 8 SDK and compile for iOS 7.
// http://stackoverflow.com/questions/24750158/autoresizing-issue-of-uicollectionviewcell-contentviews-frame-in-storyboard-pro
// http://stackoverflow.com/questions/19132908/auto-layout-constraints-issue-on-ios7-in-uitableviewcell
// bug?
if (!UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
// only one of these statements is needed
this.ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
this.ContentView.Bounds = new RectangleF (0, 0, 99999, 99999);
}
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[fullNameLabel]", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[genderLabel]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[birthdateLabel]", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("V:|-(8)-[fullNameLabel]-(5)-[genderLabel]-(5)-[birthdateLabel]-(8)-|", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraint (NSLayoutConstraint.Create (iNumberLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.CenterY, 1, 0));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:[iNumberLabel]-|", (NSLayoutFormatOptions)0, null, viewsDictionary));
didSetupConstraints = true;
}
base.UpdateConstraints ();
}
private void CreateView()
{
fullNameLabel = new UILabel () {
Font = UIFont.BoldSystemFontOfSize(17),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
genderLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
birthdateLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
iNumberLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Right
};
ContentView.AddSubviews (fullNameLabel, genderLabel, birthdateLabel, iNumberLabel);
}
}
我的约束有问题吗,还是这是另一个iOS 8错误?顺便说一句:我正在将Xcode 6.1与iOS 8.1 SDK一起使用,并希望将iOS 7作为iOS 8设备支持。 iOS 7是另一个故事。
最佳答案
在viewWillTransitionToSize
函数中,将其设置为self.tableView.estimatedRowHeight = your estimated row height
似乎可以解决该问题,这也许是来自Apple的错误,可以在以后进行改进。
关于ios - 在旋转时,使用自动布局时,iOS 8上的单元格高度会丢失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26881764/