我正在使用内置自定义元素的UITable视图,我无法将图片保留在我最初放置的位置。
我只是关注本网站的教程Team tree house
这就是我的设计外观以及它是如何在模拟器中出现的。请注意,我没有使用Autolayout,因为视频中的人说我们不应该这样做。
我尝试更改项目以使用Autolayout并添加约束,但这也不起作用。
我添加了一些自定义代码,以确保控件保持不变但没有。
这就是我的代码。
EntryListTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
EntryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
DiaryEntry *entry = [self.fetchResultsController objectAtIndexPath:indexPath];
[cell configureCellItemProperties];
[cell configureCellForEntry:entry];
[cell fixImage];
return cell;
}
EntryCell.m(仅限相关方法)
+ (CGFloat) heightForEntry: (DiaryEntry *) entry {
const CGFloat topMargin = 35.0f;
const CGFloat bottomMargin = 95.0f;
const CGFloat minHeight = 106.0f;
//The actual size of the label devided by 2 otherwise is not going to get it right.
const CGFloat originalLabelWidth = 202.0f;
CGSize constraint = CGSizeMake(originalLabelWidth, CGFLOAT_MAX);
NSStringDrawingOptions options = (NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin);
UIFont *font = [UIFont defaultFontMedium];
NSDictionary <NSString *, id> *attributes = @{NSAttachmentAttributeName: font};
CGRect boundingBox = [entry.body boundingRectWithSize:constraint options:options attributes:attributes context:nil];
return MAX(minHeight, (CGRectGetHeight(boundingBox) + topMargin + bottomMargin));
}
- (void) configureCellItemProperties {
[self.dateLabel setFont:[UIFont defaultFontBoldMedium]];
self.dateLabel.textColor = [UIColor portgoreColor];
[self.bodyLabel setFont:[UIFont defaultFontMedium]];
self.bodyLabel.textColor = [UIColor portgoreColor];
[self.bodyLabel setNumberOfLines:0];
[self sizeToFit];
[self.locationLabel setFont:[UIFont defaultFontMedium]];
self.locationLabel.textColor = [UIColor silverColor];
}
- (void) configureCellForEntry: (DiaryEntry *)entry {
self.bodyLabel.text = entry.body;
self.locationLabel.text = entry.location;
NSDateFormatter *dateFormatter = [NSDateFormatter defaultWeekMonthDayYear];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:entry.date];
self.dateLabel.text = [dateFormatter stringFromDate:date];
if (entry.image) {
self.mainImageView.image = [UIImage imageWithData:entry.image];
}else {
self.imageView.image = [UIImage imageNamed:@"icn_noimage"];
}
if (entry.mood == DiaryEntryMoodGood) {
self.moodImageView.image = [UIImage imageNamed:@"icn_happy"];
} else if (entry.mood == DiaryEntryMoodAverage){
self.imageView.image = [UIImage imageNamed:@"icn_average"];
} else if (entry.mood == DiaryEntryMoodBad) {
self.imageView.image = [UIImage imageNamed:@"icn_bad"];
}
}
- (void) fixImage {
CGRect contentBounds = self.contentView.bounds;
CGRect imageFrame = self.imageView.frame;
imageFrame.origin.x = contentBounds.origin.x + 8;
imageFrame.origin.y = contentBounds.origin.y;
self.imageView.frame = imageFrame;
}
非常感谢您的帮助:)
答案 0 :(得分:0)
你要自己踢。
在
if (entry.image) {
self.mainImageView.image = [UIImage imageWithData:entry.image];
}else {
self.imageView.image = [UIImage imageNamed:@"icn_noimage"];
}
您在imageView
声明中设置了单元格的mainImageView
图片,而不是else
的图片。
设置moodImageView
图像时也会遇到同样的错误。
希望有所帮助!