ios:自定义UITableViewCell是空白的

时间:2013-07-09 18:25:32

标签: ios uitableview

我无法弄清楚为什么它不会加载我的内容。这是单元格的.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

这是tableview中加载行的函数。如果您注意到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;
}

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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;

}