コンテンツが読み込まれない理由がわかりません。セルの .m ファイルと .h ファイルを次に示します (.xib ファイルもあります)。
TCMExploreLevelCell.h
#import <Foundation/Foundation.h>
@interface TCMExploreLevelCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *levelImage;
@property (weak, nonatomic) IBOutlet UILabel *levelTitle;
@property (weak, nonatomic) id controller;
@property (weak, nonatomic) UITableView *owningTableView;
@end
TCMExploreLevelCell.m
#import "TCMExploreLevelCell.h"
@implementation TCMExploreLevelCell
- (void)awakeFromNib
{
// Remove automatic constraints
for (UIView *v in [[self contentView] subviews]){
[v setTranslatesAutoresizingMaskIntoConstraints:NO];
}
NSDictionary *names = @{@"image":[self levelImage],
@"title":[self levelTitle]
};
NSString *fmt = @"H:|-10-[image(==42)]-[title]-10-|";
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:fmt
options:0
metrics:nil
views:names];
[[self contentView] addConstraints:constraints];
NSArray * (^constraintBuilder)(UIView *, float);
constraintBuilder = ^(UIView *view, float height){
return @[
// Constraint 0: Center Y of incoming view to contentView
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:[self contentView]
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0],
// Constraint 1: Pin width of incoming view to constant height
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:height]
];
};
constraints = constraintBuilder([self levelImage],50);
[[self contentView] addConstraints:constraints];
constraints = constraintBuilder([self levelTitle],21);
[[self contentView] addConstraints:constraints];
}
@end
行をロードするテーブルビューの関数は次のとおりです。NSlog に気付いた場合、最初の NSlog は正しいレベルのタイトルを返します。2 番目はセルのインスタンスを返しますが、levelTitle テキストを設定した後、3 番目の NSlog で null を返します。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TCMLevelRemote *l = [[[[TCMExhibitFeedStore sharedStore] allLevels] objectForKey:@"levels"] objectAtIndex:[indexPath row]];
NSLog(@"%@",[l level]);
TCMExploreLevelCell *c = [tableView dequeueReusableCellWithIdentifier:@"TCMExploreLevelCell"];
if(!c){
c = [[TCMExploreLevelCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"TCMExploreLevelCell"];
}
NSLog(@"%@",c);
[c setController:self];
[c setOwningTableView:tableView];
[[c levelTitle] setText:[l level]];
NSLog(@"%@",[c levelTitle]);
if ([l levelid] == 1){
[[c levelImage] setImage:[UIImage imageWithContentsOfFile:@"lowerlevel.png"]];
} else if ([l levelid] == 6) {
[[c levelImage] setImage:[UIImage imageWithContentsOfFile:@"alllevels.png"]];
} else {
[[c levelImage] setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"level%d.png",[indexPath row]]]];
}
return c;
}