【发布时间】:2014-04-18 15:49:32
【问题描述】:
我的故事板中有一个单元的原型。
基本上,单元格分为两部分:
- 文本视图
- 图像视图
单元格被初始化,其值将对象传递给单元格本身。在该方法中,单元格将其元素设置为传递的值。
主要思想是根据图像或文本的存在来更改单元格布局。为此,我过度约束了UITextView 和UIImageView,一方面添加了两个具有不同优先级的约束。优先级将根据传递的对象而变化。
例如UITextView 有两条虚线,它们代表具有不同优先级和不同常量值的两个约束。第一个(从 LtoR 读取)的优先级为 910,常数为 156,第二个的优先级为 900,常数为 20。
在故事板编辑器中,如果我交换 2 个优先级,我可以直观地获得我想要实现的目标。
单元格代码:
- (void) updateConstraints {
if (self.postObject.timelinePostText && self.postObject.timelinePostMultimedia) {
self.imageMinusTextViewConstraint.priority = 900;
self.imagePlusTextViewConstraint.priority = 910;
self.textViewMinusImageViewConstraint.priority = 900;
self.textViewPlusImageViewConstraint.priority = 910;
}
else if (self.postObject.timelinePostMultimedia) {
self.imageMinusTextViewConstraint.priority = 910;
self.imagePlusTextViewConstraint.priority = 900;
}
else if (self.postObject.timelinePostText) {
self.textViewMinusImageViewConstraint.priority = 910;
self.textViewPlusImageViewConstraint.priority = 900;
}
[super updateConstraints];
}
- (void) configureCellWith: (AFTimelinePostObject*) postObject {
self.postObject = postObject;
self.userNameLabel.text = postObject.timelinePostOriginalPoster;
self.dateLabel.text = [[AFTimelinePostObject dateFormatterInternet] stringFromDate:postObject.timelinePostDateObject];
self.postTitleLabel.text = postObject.timelinePostTitle;
self.multimediaMediaType = postObject.timelinePostMultimedia ? [self checkMediaTypeAtPath: postObject.timelinePostMultimedia] : kMediaTypeUnknown;
if (postObject.timelinePostMultimedia && postObject.timelinePostText) {
[self.postImageView setImageWithURL:[NSURL URLWithString:postObject.timelinePostMultimedia] placeholderImage:nil];
self.postTextView.text = postObject.timelinePostText;
}
else if (postObject.timelinePostMultimedia) {
[self bringSubviewToFront:self.postImageView];
[self.postImageView setImageWithURL:[NSURL URLWithString:postObject.timelinePostMultimedia] placeholderImage:nil];
}
else if (postObject.timelinePostText) {
[self bringSubviewToFront:self.postTextView];
self.postTextView.text = postObject.timelinePostText;
self.postImageView.image = nil;
}
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
[self setNeedsLayout];
[self layoutIfNeeded];
}
删除视图不是解决方案,因为单元格被回收了,如果需要我应该重新添加它,但是这样我想我错过了与回收相关的性能优势。
不幸的是,单元格仍然平等地分为文本和图像,没有根据传递的数据改变它们的框架。
我错过了什么?
【问题讨论】:
标签: ios uitableview autolayout