我是iOS软件开发工具包的新手,所以我正在努力学习如何正确地做事情,所以除了我的问题之外,请随时指出我做事情的方式中的任何缺陷。另外,我使用的是Xamarin框架,但我或多或少可以将Obj-C转换成我正在做的事情,所以两个答案都可以。
我正在创建一个与UITableViewCellStyleSubtitle
非常相似的自定义单元格。事实上,唯一的区别是我想要一个稍微大一点的高度单元格,在单元格的右侧有一个日历图标。为了制作日历图标,我有两个UILabels,它们分别作为日历的上半部分和下半部分。这样,我就可以动态地将月份和日期的文本放入其中:
public pendingMessagesCell (NSString cellId) : base(UITableViewCellStyle.Subtitle, cellId)
{
_calendarBottom = new UILabel () {
TextColor = UIColor.White,
BackgroundColor = UIColor.Gray,
TextAlignment = UITextAlignment.Center
};
_calendarTop = new UILabel () {
TextColor = UIColor.White,
BackgroundColor = UIColor.DarkGray,
TextAlignment = UITextAlignment.Center
};
按照this堆栈溢出问题中的指导,我在TableViewController
中使用了EstimatedRowHeigh
和UITableView.AutomaticDimension
:
public class pendingMessageTableViewController : UITableViewController
{
/// <summary>
/// Parent Controller.
/// </summary>
private pendingViewController _controller;
/// <summary>
/// Messages pending
/// </summary>
private IList<Message> pendingMessages;
/// <summary>
/// Initializes a new instance of the <see cref="SaltAndPepper.ios.pendingMessageTableViewController"/> class.
/// </summary>
public pendingMessageTableViewController (UITableViewStyle style) : base(style)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
pendingMessages = new List<Message> ();
pendingMessages = MessageManager.GetMessages ();
var source = new pendingTableMessageViewSource (pendingMessages);
TableView.Source = source;
TableView.RowHeight = UITableView.AutomaticDimension;
TableView.EstimatedRowHeight = 66;
TableView.TableFooterView = new UIView(CoreGraphics.CGRect.Empty);
this.AutomaticallyAdjustsScrollViewInsets = false;
}
然后在我的UITableViewSource
子类中,我这样覆盖GetCell:
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
var row = indexPath.Row;
var cell = tableView.DequeueReusableCell (_Cell) as pendingMessagesCell;
if (cell == null) {
cell = new pendingMessagesCell ((NSString)_Cell);
}
UIImage img = GetContactInfo ();
cell.UpdateCell (_data [indexPath.Row].Recipient.ToString (), _data [indexPath.Row].Text.ToString (), "Aug", "12", img);
cell.SetNeedsUpdateConstraints ();
return cell;
}
最后,在我的UITableViewCell
子类中,我在构造函数中声明:
TextLabel.TranslatesAutoresizingMaskIntoConstraints = false;
DetailTextLabel.TranslatesAutoresizingMaskIntoConstraints = false;
_calendarTop.TranslatesAutoresizingMaskIntoConstraints = false;
_calendarBottom.TranslatesAutoresizingMaskIntoConstraints = false;
DetailTextLabel.LineBreakMode = UILineBreakMode.TailTruncation;
ContentView.AddSubviews (new UIView[]{ _calendarBottom, _calendarTop });
然后,在上面提到的关于动态单元格高度的stackoverflow链接后面添加约束。
我的问题是:我一直试图使用自动布局来使整个类具有约束,但对于ImageView的特定情况,我不想使用约束,因为我喜欢UITableViewCells ImageView的默认实现(某种程度上)。首先,我想要一个圆形的图像,而不是UITableViewCell.ImageView
的默认正方形图像。如果我继续使用:
ImageView.Layer.CornerRadius = ImageView.Frame.Size.Width / 2;
ImageView.ClipsToBounds = true;
在我的Cell子类的LayoutSubview
中,图片(从contacts thumbnailImageData获得)是圆形的,但对我来说有点大:
因此,我尝试在ImageView左侧添加对TextLabel和DetailedTextLabel的约束,并使用ImageView.Frame =ImageView CoreGraphics.CGRect ( ImageView.Frame.X,(ContentView.Frame.Height / 2) - ((ImageView.Frame.Height * .75)/2),ImageView.Frame.Width * .75,ImageView.Frame.Height *.75)调整帧大小;
然后bam图像以我想要的大小居中。(约束在UpdateConstraints中完成,图像大小调整在LayoutSubViews中完成):
当我查看有关这方面的表单信息时,我遇到了一个问题HERE,它说我不应该在单元格的样式项上使用约束,比如TextLabel等等,所以我很困惑该如何去做。我喜欢大多数定位的字幕单元格样式,只是想要一些细微的调整。我是以一种糟糕的方式来做这件事,还是应该用自建的UILables约束所有的事情。如果是这样的话,有没有关于字幕单元格样式默认位置的文档供我参考?我还没找到。
发布于 2015-10-03 06:16:41
因此,如果您想要更简单的路线,而不是重新创建一个全新的Cell,只需将附件视图分配给您自己的自定义视图,即您的Calendar视图。将自动调整所有内容。
如果你想继续你现在的方式,只需保留你的自定义单元格,但再次将你的日历移到附件视图中。只要你先设置它的大小,它就会很完美。
https://stackoverflow.com/questions/32904161
复制相似问题