2012-11-21 87 views
2

我有一個UITableViewController,它具有帶自定義單元格的表格視圖(它們有一個子類和一個xib)。問題是我從一個博客加載數據,並且它顯示正常,但是當我嘗試滾動或觸摸時,單元格重置,就像它們沒有加載一樣。UITableView自定義單元格在嘗試滾動時消失

這是怎麼出現的: enter image description here

這是發生了什麼,當我嘗試滾動: enter image description here

這是我的視圖控制器代碼:

#import "TLDRCategoryViewController.h" 

@interface TLDRCategoryViewController() 
@property(nonatomic,strong) IBOutlet UIView *headerView; 
@property(nonatomic,strong) NSMutableArray *posts; 
@property(nonatomic, assign, readonly) TLDRTag category; 
@property(nonatomic, strong) IBOutlet UILabel *category_label; 
@end 

@implementation TLDRCategoryViewController 
@synthesize headerView = _headerView, posts = _posts, category = _category, category_label = _category_label; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

-(void)setCategory:(TLDRTag)_cat { 
    _category = _cat; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.tableView.tableHeaderView = _headerView; 

    _category_label.text = [TLDRHelper showableNameForTag:_category]; 
    _category_label.textColor = [TLDRHelper colorForTag:_category]; 

    TLDRRetriever *retriever = [[TLDRRetriever alloc] initWithDelegate:self]; 
    [retriever postsByCategory:_category]; 
    if (_posts == nil) 
     _posts = [[NSMutableArray alloc] init]; 


} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)postsLoaded:(NSDictionary *)postsDict { 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { 
     for (NSDictionary *key in postsDict[@"response"][@"posts"]) { 
      TLDRNewsItem *newsPost = [[TLDRNewsItem alloc] initWithDictionary:key]; 
      [self.posts addObject:newsPost]; 
     } 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      [self.tableView reloadData]; 
     }); 

    }); 

} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    return [self.posts count]; 
} 

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

    static NSString *CellIdentifier = @"TLDRNewsCell"; 

    TLDRNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil){ 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TLDRNewsCell" owner:nil options:nil]; 
     for(id currentObject in topLevelObjects) { 
      if([currentObject isKindOfClass:[TLDRNewsCell class]]) { 
       cell = (TLDRNewsCell *)currentObject; 
       break; 
      } 
     } 
    } 


    cell.title.text = ((TLDRNewsItem*)self.posts[indexPath.row]).articleTitle; 
    cell.content.text = ((TLDRNewsItem*)self.posts[indexPath.row]).articleTLDR; 

    return cell; 
} 

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *text = ((TLDRNewsItem*)self.posts[indexPath.row]).articleTLDR; 

    CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)]; 

    return textSize.height + PADDING * 3; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 

這是TLDRRetriever的相關部分:

-(void)postsByCategory:(NSInteger)cat { 
    NSString *string_cat = @""; 

    switch (cat) { 
     case TLDRTagWorldNews: 
      string_cat = @"world%20news"; 
      break; 
     case TLDRTagBusiness: 
      string_cat = @"business"; 
      break; 
     case TLDRTagDesign: 
      string_cat = @"design"; 
      break; 
     case TLDRTagEntertainment: 
      string_cat = @"entertainment"; 
      break; 
     case TLDRTagFacts: 
      string_cat = @"facts"; 
      break; 
     case TLDRTagHealth: 
      string_cat = @"health"; 
      break; 
     case TLDRTagPolitics: 
      string_cat = @"politics"; 
      break; 
     case TLDRTagScience: 
      string_cat = @"science"; 
      break; 
     case TLDRTagSports: 
      string_cat = @"sports"; 
      break; 
     case TLDRTagTech: 
      string_cat = @"tech"; 
      break; 
     case TLDRTagWeb: 
      string_cat = @"web"; 
      break; 

     default: 
      string_cat = @""; 
      break; 
    } 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 

    dispatch_async(queue, ^{ 
     NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kTLDRAPIURLCats, string_cat]]]; 
     NSError *error; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

     [[self delegate] postsLoaded:json]; 

    }); 



} 

我有一個幾乎精確的視圖控制器在該應用程序以及它工作正常(我使用該控制器的大部分代碼,這一個將可重用的其他類似的類別)。這個控制器是批量分配的(我分配了11個控制器並將它們保存在一個數組中)。我無法調試這個,所以我不知道發生了什麼。代碼中有錯嗎?謝謝!

+0

是否在TLDRNewsCell中重寫了prepareForReuse方法? – BergP

+0

nope,TLDRNewsCell沒有任何內容超過默認值並且合成了兩個IBOutlets – pmerino

回答

7

修復與控制器或類似的東西沒有關係:它是根視圖控制器。意見沒有被保留在內存中,所以我只是爲每個控制器製作了一個ivar,而不是循環遍歷它們,並修復了應用程序:)

+0

感謝你們,我遇到了同樣的問題。我仍在適應ARC隨機清理我的物品。 – Jameson

+1

Yikes!我假設我的表視圖控制器將被保留,如果我將其視圖添加到層​​次結構,但它看起來像控制器必須單獨保留。 – Eliot

相關問題