【问题标题】:UITableViewCell image anchored to the topUITableViewCell 图像锚定到顶部
【发布时间】:2016-06-29 17:55:41
【问题描述】:

我正在使用内部包含自定义元素的 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;

}

非常感谢您的帮助:)

【问题讨论】:

    标签: ios objective-c iphone xcode uitableview


    【解决方案1】:

    你会踢自己的。

    if (entry.image) {
        self.mainImageView.image = [UIImage imageWithData:entry.image];
    }else {
        self.imageView.image = [UIImage imageNamed:@"icn_noimage"];
    }
    

    您在else 语句中设置单元格的imageView 图像,而不是mainImageView 的图像。

    在设置moodImageView 的图片时,您也遇到了同样的错误。

    希望有帮助!

    【讨论】:

    • 你是英雄!伙计,你怎么注意到的?从昨天开始,我一直在尝试解决这个问题。 :)
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    相关资源
    最近更新 更多