2013-07-09 74 views
0

我不明白爲什麼它不會加載我的內容。我這裏還有了.M和.h文件的小區(有一個的.xib文件以及)ios:自定義UITableViewCell是空的

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。如果您注意到NSlogs,第一個返回正確的級別標題。第二返回小區的一個實例,但是設置levelTitle文本之後返回第三的NSLog

- (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; 
} 
+0

嘗試在-tableView上設置它們:willSelectRowAtIndexPath: –

+0

我不需要在表構建時設置視圖嗎? – LoneWolfPR

回答

1

如果您的電池有廈門國際銀行文件,然後您註冊在viewDidLoad中該文件:

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

然後,在cellForRowAtIndexPath中,使用dequeueReusableCellWithIdentifier:forIndexPath:而不是dequeueReusableCellWithIdentifier :.你不需要if(cell == nil)子句,該方法保證給你一個單元格。這是使用基於xib的單元格的推薦方式,而不是在cellForRowAtIndexPath中加載nib。

+0

我做了一件非常類似於此事的事情。 – LoneWolfPR

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; 

}