0

我不知道为什么它不会加载我的内容。这是单元格的 .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,第一个会返回正确的级别标题。第二个返回单元格的实例,但是在设置 levelTitle 文本后,它在第三个 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;
}
4

2 回答 2

1

如果您的单元格有一个 xib 文件,那么您在 viewDidLoad 中注册该文件:

[self.tableView registerNib:[UINib nibWithNibName:@"TCMExploreLevelCell" bundle:nil] forCellReuseIdentifier:@"TCMExploreLevelCell"];

然后,在 cellForRowAtIndexPath 中,使用 dequeueReusableCellWithIdentifier:forIndexPath: 代替 dequeueReusableCellWithIdentifier:。您不需要 if (cell == nil) 子句,该方法保证会给您一个单元格。这是使用基于 xib 的单元格的推荐方法,而不是在 cellForRowAtIndexPath 中加载 nib。

于 2013-07-09T18:46:55.037 回答
0

您应该像这样loadNibNamed:cellForRowAtIndexPath:委托方法中使用该方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell.xib" owner:self options:nil];

        for(id currentObject in nibArray){
            if([currentObject isKindOfClass:[CustomCell class]]){
                cell = (CustomCell *)currentObject;
                break;
            }
        }
    }

return cell;

}

于 2013-07-09T18:45:17.873 回答