2016-06-29 7 views
0

内部にカスタム要素を含むUITableビューを使用しています。元の場所に配置するために画像を取得できません。UITableViewCell画像が上部に固定されています

することは、私はちょうどこのウェブサイトTeam tree house

これは私のデザインがどのように見えるかで、どのように彼はシミュレータint型、それが出て来るのチュートリアルから、次のい。私はAutolayoutを使用していないことに注意してください。

Current status

私は、自動レイアウトを使用するプロジェクトを変更し、制約を追加しようとしたが、それはどちらか動作しません。

カスタムコードを追加して、コントロールをそのまま残しておくことを確認しました。

これは私のコードのようです。

助けを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

あなたがしようとしていますあなた自身を蹴る。

あなたは else文でセルの imageView画像ではなく、 mainImageViewの画像を設定している

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

moodImageViewの画像を設定するときにも同じバグがあります。

希望に役立ちます!

+0

あなたは英雄です!あなたはどうやってそれに気づきましたか?私は昨日からこれを解決しようとしてきました。 :) –

+0

¯\ _(ツ)_ /¯大歓迎です!それがうれしかった。 – stefandouganhyde

関連する問題