2014-10-10 41 views
1

当我运行该应用程序时,标题视图容器空间在那里,并且是可见的。我试图在标题视图中显示用户名,但没有显示出来。这里是我的代码中的tableView至今:没有显示自定义标题查看

#import "HomeView.h" 

@interface HomeView() <UIActionSheetDelegate, UIImagePickerControllerDelegate> 

@end 

@implementation HomeView 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    if ([PFUser currentUser]) { 
     NSLog(@"Welcome to the App, %@", [self.user objectForKey:@"username"]); 
    } else { 
     LoginView *loginView = [[LoginView alloc] init]; 
     [loginView setHidesBottomBarWhenPushed:YES]; 
     [loginView.navigationItem setHidesBackButton:YES]; 
     [self.navigationController pushViewController:loginView animated:NO]; 
    } 

    UIBarButtonItem *takePhoto = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(takePhoto:)]; 
    self.navigationItem.rightBarButtonItem = takePhoto; 

// [self.tableView registerClass:[HomeViewCell class] forCellReuseIdentifier:@"cellIdentifier"]; 
// [self.tableView registerNib:[UINib nibWithNibName:@"HomeViewCell" bundle:nil] 
//   forCellReuseIdentifier:@"cellIdentifier"]; 
    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 
} 

- (id)initWithStyle:(UITableViewStyle)style { 
    self = [super initWithStyle:style]; 
    if (self) { 
     self.reusableSectionHeaderViews = [NSMutableSet setWithCapacity:3]; 

    } 
    return self; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    PFQuery *query = [PFQuery queryWithClassName:@"UserPhotos"]; 
    [query orderByDescending:@"createdAt"]; 
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (error) { 
      NSLog(@"Error: %@ %@", error, [error userInfo]); 
     } else { 
      self.userPhotos = objects; 
      NSLog(@"Retrieved objects: %@", self.userPhotos); 
      [self.tableView reloadData]; 
     } 
    }]; 
} 


#pragma mark - Table view data source 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return 1; 
} 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    if (section == self.userPhotos.count) { 
     // Load More section 
     return nil; 
    } 

    HomeHeaderView *headerView = [self dequeueReusableSectionHeaderView]; 

    if (!headerView) { 
     headerView = [[HomeHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 44.0f) buttons:HomePhotoHeaderButtonsDefault]; 
     headerView.delegate = self; 
     [self.reusableSectionHeaderViews addObject:headerView]; 
    } 

    //Setting the username display. 
    PFObject *owner = [self.userPhotos objectAtIndex:section]; 
    PFUser *user = [owner objectForKey:@"user"]; 
    [user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) { 
     if (!error) { 
      NSString *username = user.username; 
      [headerView.userButton setTitle:username forState:UIControlStateNormal]; 
     } 
    }]; 

    return headerView; 
} 

- (HomeHeaderView *)dequeueReusableSectionHeaderView { 
    for (HomeHeaderView *sectionHeaderView in self.reusableSectionHeaderViews) { 
     if (!sectionHeaderView.superview) { 
      // we found a section header that is no longer visible 
      return sectionHeaderView; 
     } 
    } 

    return nil; 
} 


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

    static NSString *CellIdentifier = @"cellIdentifier"; 
    HomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) 
    { 
     cell = [[HomeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    //Setting the image in the cell. 
    PFObject *carPhoto = [self.userPhotos objectAtIndex:indexPath.row]; 
    PFFile *imageFile = [carPhoto objectForKey:@"imageFile"]; 
    NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url]; 
    NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl]; 
    cell.carImage.contentMode = UIViewContentModeScaleAspectFit; 
    cell.carImage.image = [UIImage imageWithData:imageData]; 


    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 269; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if (section == self.userPhotos.count) { 
     return 0.0f; 
    } 
    return 44.0f; 
} 
+0

为什么要保留一个标题视图数组,而不是让系统通过使用 来为你做这件事(比如它对于单元格)dequeueReusableHeaderFooterViewWithIdentifier:? – rdelmar 2014-10-10 00:28:56

+0

@rdelmar我会在使用下面的答案之前尝试这种方法。感谢您的建议,没有想到这一点。如果我遇到问题,我会通知你。 – 2014-10-10 01:32:23

+0

@rdelmar在我的自定义标题视图应该使用以下风格的实际标题视图? ' - (id)initWithStyle:(UITableViewHeaderFooterView *)style reuseIdentifier:(NSString *)reuseIdentifier' – 2014-10-10 02:15:33

回答

0
[user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) { 
    if (!error) { 
     NSString *username = user.username; 
     [headerView.userButton setTitle:username forState:UIControlStateNormal]; 
    } 
}]; 

上面的代码,你正在更新从后台线程的headerview UI。您需要包装UI部分,以便在主线程上执行。

dispatch_async(dispatch_get_main_queue(), ^{ 
    [headerView.userButton setTitle:username forState:UIControlStateNormal]; 
}; 

但是,您需要小心,您正在执行后台操作,然后更新您正在重用的标头。在操作完成之前,可能会重新使用标题作为新节,然后使用该节的错误用户名进行设置。如果标题在操作完成之前滚动出屏幕,您可以不重复使用标题或取消网络操作。

+0

嗨克里斯,现在我所有的标题视图显示的用户名,除了第一个它只是空白,直到我从屏幕上滚动它,然后回去,它显示了..我该怎么做,以确保在加载它显示用户名从一开始。谢谢! – 2014-10-15 19:18:40