0

と があるUITableViewCellnib ファイルがありUIImageViewますUILabel。これらの両方のコントローラーへのアウトレットと、セル自体のアウトレットがあります。プログラムでラベルと画像を設定していますが、画像が表示されません。

そこで試しに行ってみたのですが、画像自体をnibファイルにセットしても表示されません。背景色を設定すると、うまく表示されます。何か案は?私は立ち往生しています。

nibファイルを介しても機能しないため、私の考えでは、コードとは無関係のようです。しかし、何らかの形で役立つ場合に備えて、とにかくここにあります。

MyViewController.h

@interface MyViewController : UITableViewController

@property (strong, nonatomic) MyModel *myModel;
@property (strong, nonatomic) NSArray *tableViewCells;

@property (strong, nonatomic) IBOutlet UITableViewCell *tableViewCell;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIImageView *myImage;

@end

MyViewController.m

@interface MyViewController ()

- (void)bindMyModel:(MyModel*)model toView:(UITableViewCell*)view;

- (UITableViewCell*)copyUITableViewCell:(UITableViewCell*)cell;

@end

@implementation MenuViewController

@synthesize myModel = _myModel;
@synthesize tableViewCells = _tableViewCells;

@synthesize tableViewCell = _tableViewCell;
@synthesize myLabel = _myLabel;
@synthesize myImage = _myImage;

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableArray *cells = [[NSMutableArray alloc] init];
    [MyModel loadAndOnSuccess:^(id data, id context) {
        self.myModel = data;
        for (MyModel *item in self.myModel.items) {
            [[NSBundle mainBundle] loadNibNamed:@"TableCellNib" owner:self options:nil];
            [self bindMyModel:item toView:self.tableViewCell];
            [cells addObject:[self copyUITableViewCell:self.tableViewCell]];
            self.tableViewCell = nil;
        }
        self.tableViewCells = [[NSArray alloc] initWithArray:cells];
    } onFail:^(NSString *error, id context) {
        NSLog(@"FAIL with error: %@", error);
    }];
}

- (void)viewDidUnload
{
    [self setTableViewCell:nil];
    [self setMyLabel:nil];
    [self setMyImage:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void) bindMyModel:(MyModel*)model toView:(UITableViewCell*)view
{
    if (view) {
        self.myLabel.text = model.myLabelText;
        self.myImage.image = model.myImageResource;
        self.myLabel = nil;
        self.myImage = nil;
    }
}

- (UITableViewCell*)copyUITableViewCell:(UITableViewCell*)cell
{
    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: cell];
    return [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
}

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.tableViewCells objectAtIndex:indexPath.row];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myModel.items.count;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Irrelevant code here
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ((UITableViewCell*)[self.tableViewCells objectAtIndex:indexPath.row]).frame.size.height;
}

@end
4

1 に答える 1

1

IBOutletで 2 つのを使用して、 の一部でUITableViewControllerある多数UILabelの およびを入力しようとしています。のカスタム サブクラスを作成し、そのサブクラスにを追加する必要があります。UIImageViewUITableViewCellUITableViewCellIBOutlet

@interface CustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIImageView *myImage;

@end

それからあなたのbindMyModel:toView:

- (void) bindMyModel:(MyModel*)model toView:(CustomCell*)view
{
    if (view) {
        view.myLabel.text = model.myLabelText;
        view.myImage.image = model.myImageResource;
    }
}

IBOutletこれで、セルごとに独立したがあります。また、バインディングの一部も変更する必要があります。これは修正ですが、正直なところ、多くのコードを書き直して呼び出しで使用dequeueReusableCellWithIdentifierし、再利用する s のcellForRowAtIndexPathプールを保持し、呼び出しで and を設定するCustomCellだけです。myLabel.textmyImage.imagecellForRowAtIndexPath

于 2012-06-29T20:01:27.560 に答える